1. Processes and threads

A) Process: it is the basic unit of the system for resource allocation and scheduling. I. Every PHP script that is run starts a process. In nginx: php-fpm 2. in apache: CGI b) processes include threads, which are an execution unit of our program responsible for executionCopy the code

2. PHP multi-threaded development

A) Concept: refers to the technology of realizing concurrent execution of multiple threads from software or hardware. A computer with multithreading capability can execute more than one thread at a time due to hardware support, thus improving the overall processing performance b) Principle: in a PHP process, open multiple threads to perform tasks, improve the execution efficiency I. The same thing, one person and ten people to do the efficiency is not the same, multithreading is the principle 1. Example: It takes 5-10 minutes to clean ten clothes on average, so it takes one person 100 minutes to wash all the clothes. So if ten people wash the clothes together, one for each person, the washing of the ten clothes will be the slowest washing event of the ten people. C) Implementation: PHP uses the PThreads extension, which is a PHP extension written based on C for manipulating the computer's thread I. Create a class that inherits the Thread class and implement the following methods 1.run () : write business code for multithreaded execution ii.start () : Start multithreading and execute the task in Run iii.isrunning () : determine if the Thread is finished executing iv.join: Used to obtain the results of thread execution d) Disadvantages: I. Memory usage II. Starting and closing threads consumes system resources III. Threads by default share data in memory, so multithreading is not safe, which is why PHP's PThreads extension requires a thread-safe version, which blocks memory flows to IV between threads. Memory sharing represents the preemption of memory. The mutex mechanism needs to be introduced to realize memory management. When another thread accesses memory, other threads are not allowed to access it. Multi-threaded concurrent processing: 1. Execute multiple tasks at the same time to improve execution efficiency and obtain the returned result 2. Scenario: Network request, local file I/O, and big data processing II. Specific services: crawler collection, file read and write logsCopy the code

PHP generator

A) Concept: implement Iterator to reduce memory consumption b) Principle: implement the Iterator interface in PHP and implement the methods in the interface: I. rewind: reset the object pointer ii.Next: move the object pointer to the Next iii.Current: Obtain the value iv.Key: obtain the Key of the current pointer v. aild: verify that the iteration condition meets vi. To realize the iteration of the object, the program obtains the object of the generator, occupying the space of only one object in memory, and updates the value in the object through iteration, thus reducing the memory consumption c) application: If we use the yield keyword in a function and call the function again, we will not return the result of the function execution, but a generator object. We can iterate over this object through foreach. Each iteration will fetch the value after yield in the generator, and the generator will pause at yield. The next loop continues from the paused positionCopy the code

D) Scenario: I. Read and write large files to reduce memory consumptionCopy the code

4. Coroutine:

A) principle: coroutines is less than the thread, different coroutines does not depend on system scheduling, but through the code to implement multitasking quickly switch, the switch can't perceive the process of sensory was conducted at the same time, is actually a serial execution, because they don't need a system to regulate, so special fast switching special less resources consumption, and can improve the efficiency of execution I again. Parallelism: a single-core CPU can only perform one task at a time. If it is multi-core, it can perform multiple tasks at the same time, thus improving the execution efficiency II. Concurrency: single-core can quickly switch tasks, release and receive CPU control, and realize multi-task switching and concurrent execution b) Implementation: realize coroutine through PHP generator + task manager + scheduler c) Application scenario: I. Network requests, I/O requests, improving system throughput and execution efficiency II. Because coroutines occupy less system resources, they can process more requests and improve system throughputCopy the code

5. Gorountine concurrent programming of Go

A) Concept: Gorountine principle is a coroutine, but the scheduling of these coroutines is already implemented internally, including garbage collection, task management and scheduling. Therefore, we develop internal implementation of coroutines without management, just focus on business development. Go implements concurrency from the language level. Multi-task fast switching, no need for operating system scheduling, communication can be achieved through channels c) : I. Go func() {}() 2.Fun := func() {} go Fun () ii Chan := make(Chan string[, Chan length (int)],) a) make(Chan string[, Chan length (int)],) a) A)Chan<- "value" b) value := <-chan iii.sync 4. Wg.wait () : check whether the value of the wg counter is 0, if it is 0, continue to execute, otherwise block.D) Application scenario: I Network requests ii. local file I/O, log processing iii. coroutines are commonly used to implement asynchronous operationsCopy the code