I have around 10 different requests for different servers and used to get the response sequentially in respective to the function call in my website.
Now, I want to call my function in parallel for different server request. Once I get the first response from any of the servers, then want to stop the the remaining processes.
Currently I am calling my function like so:
my_launch(1);
my_launch(2);
my_launch(3);
my_launch(4);
my_launch(5);
my_launch(6);
my_launch(7);
my_launch(8);
my_launch(9);
my_launch(10);
This executes sequentially; how can I run this code in parallel using PCNTL?
About PCNTL availibility
PHP has some PCNTL features built in but they are not enabled by default.
From php.net pcntl installation instructions:
Regarding parallelization
If you know the number of cores on your machine, then I would suggest dividing the work into that many divisions. Process control only works on *nix-like systems, so you probably have the
nproccommand available:You fork to get the proper number of processes running, divide the work between them and off you go. Dividing the work might be difficult, but I’m sure you’ll figure it out.
Code dump
I was feeling generous so I went ahead and coded most of it for you. Be sure to understand it because it isn’t quite finished.
Notes:
nproc, so if you don’t have that utility you should replace it with the number of processes you want to make.Code: