What is used to implement multi-processing on the PHP server?

WBOY
Release: 2023-03-02 06:48:01
Original
881 people have browsed it

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!

Reply content:

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template