Answer the question
In order to leave comments, you need to log in
Parallelize the work of a part of the script?
Greetings.
Please help me with this moment. There is a task that takes a long time. It communicates with someone else's API, which takes most of the time. The algorithm is this:
Answer the question
In order to leave comments, you need to log in
Gearman is just that. It supports running tasks in parallel and receiving a response upon completion (aka callback) through methods (doNormal, doHigh, doLow).
It is more difficult to work with forks, but it is also possible. For each iteration, you fork a new process and write the result somewhere, the only question is how to read the overall result later. In general, I would advise you to take a closer look at gearman
2. pcntl_signal will signal the end of the child process.
When I worked with these functions for a long time, I could not get my head around completely those things. The work goes something like this:
a) We hang up the pcntl_signal handler. This is a signal about the end of some of the processes.
b) Cyclically forking, first writing the number of the data packet into a certain variable. According to the help, we determine - a copy / not a copy.
c) if the copy, posix_setsid, we create the necessary code, or through pcntl_exec we replace ourselves with the package handler.
At this point, the package handler takes the data returned by the third-party service and processes it in some way.
d) At the end of the child process, we know its PID and, accordingly, we know the index of the processed packet.
3. ps x and read the output. We know the PID and other process attributes. But not kosher.
I don’t know how relevant this is for you, but I write scripts of this type in Groovy
. You can also run threads for parallel work.
See how the data is.
If transactions are included in the database, then I would do so.
1) Forked the required number of times.
2) Transactions took a set of records from the database and processed. The records would be blocked. Well, or an anonymous transaction marked as executable (xs is it possible to do this in MySQL)
3) After closing the transaction, take the next portion.
A very cool framework for async applications in php - reactphp.org
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question