The php backend will return a large amount of data from other places and present it in arrays. The data needs to be written to the database one by one. The single process is too slow, I want to use multiple processes for implementation. I checked some information and noticed the pcntl _ * series functions, but it seems that this is only available in the command line... the php backend will return a large amount of data from other places and present it in arrays. The data needs to be written to the database one by one. The single process is too slow, I want to use multiple processes for implementation.
I checked some information and noticed the pcntl _ ** series of functions, but it seems that they can only be executed in the command line and cannot be used on the server.
Is there any way to achieve this?
Please kindly advise!
Reply content:
The php backend will return a large amount of data from other places and present it in arrays. The data needs to be written to the database one by one. The single process is too slow, I want to use multiple processes for implementation.
I checked some information and noticed the pcntl _ ** series of functions, but it seems that they can only be executed in the command line and cannot be used on the server.
Is there any way to achieve this?
Please kindly advise!
What you need is not a multi-process, but a message queue.
After receiving the request, the web server pushes a message to the message queue and then returns the browser result (for example, displaying the message in progress)
In addition, a script for consuming a message queue is run for a long time. it obtains the message from the message queue for processing, and changes the status to complete processing (if you have the concept of a task)
At this level, you need multi-process or multi-thread depending on your specific situation, multi-process or multi-thread operations can only improve IO-intensive operations (from the perspective that you need to request a large amount of data elsewhere)
Meet you: look at the pcntl_fork function.
Php_swoole is more mature and stable than pcntl. In addition, the message queue must be used for better processing.