Multiple processes(Recommended learning: PHP video tutorial)
Use multi-process, after the child process ends, the kernel Will be responsible for recycling resources
When using multiple processes, the abnormal exit of the child process will not cause the entire process Thread to exit, and the parent process will have the opportunity to rebuild the process.
A resident main process is only responsible for task distribution, and the logic is clearer.
The multi-process method is more stable, and data sharing can also be achieved using inter-process communication (IPC).
Shared memory, this method is the same as reading and writing variables between threads. It needs to be locked, and there will be synchronization and deadlock problems.
Message queue, you can use multiple sub-processes to grab the queue mode, the performance is very good
Multi-threading
Threads are in the same process, Memory variables can be shared to achieve inter-thread communication
Threads are more lightweight than processes. Opening a large number of processes will consume more system resources than threads
There are also some problems with multi-threading:
There is a synchronization problem when threads read and write variables, and locks are required
If the granularity of the lock is too large, there will be performance problems, which may result in only one thread running and other threads waiting for the lock
Use multiple locks at the same time, the logic is complex. Once a lock is not released correctly, a thread deadlock may occur
A fatal error in a thread will cause the entire process to crash
The above is the detailed content of The role of php multi-threading and multi-process. For more information, please follow other related articles on the PHP Chinese website!