Home > Article > Backend Development > What is used to implement multi-processing on the PHP server?
The PHP backend will request a large amount of data from other places and present it in the form of an array. These data need to be written to the database one by one. Single process is too slow, so I want to use multiple processes to achieve it.
Checked From some information, I noticed the pcntl_** series of functions, but it seems that these can only be executed in the command line and cannot be used on the server.
Is there any way to achieve this requirement?
Please feel free to teach me!
The PHP backend will request a large amount of data from other places and present it in the form of an array. These data need to be written to the database one by one. Single process is too slow, so I want to use multiple processes to achieve it.
Checked From some information, I noticed the pcntl_** series of functions, but it seems that these can only be executed in the command line and cannot be used on the server.
Is there any way to achieve this requirement?
Please feel free to teach me!
What you need is not multiple processes, but a message queue
After the web server receives the request, it pushes a message to the message queue, and then directly returns the browser result (for example, it shows that it is being processed)
Create another script that consumes the message queue, runs for a long time, gets the message from the message queue, processes it, and finally changes the status to processing completed (if you have the concept of a task)
At this level, whether you need multi-process or multi-thread depends on your specific situation. Multi-process or multi-thread can only improve IO-intensive operations (judging from the fact that you need to request a large amount of data from other places) Considered consistent)
To satisfy you: take a look at the pcntl_fork function
php_swoole This is more mature and stable than pcntl. In addition, it is better to cooperate with the message queue for processing