Running several processes in PHP
A PHP library to run multiples processes using JavaScript

Problem
I had to analyze and read 14739 folders using PHP, which took over half an hour.

While PHP has some async functionalities, they don’t work in Windows. However, some solutions are still available, such as using CURL calls, exec calls, and COM calls, but, these are based on calling the functionality without getting an answer. What progress does it have? What if the operation failed?
So, one solution is to use JavaScript and PHP in tandem. Why?
JavaScript could coordinate a visual interface and the async calls.
While PHP does the job.
Then I created this simple library:
EFTEC/MultiOne: MultiOne is a library to execute concurrent tasks programmed in PHP using JavaScript
How does it work?
It works in 3 stages:
Starting the jobs and creating the payload for every worker. A worker is the process that does its job.
Every worker does his job
And when every worker ends, you can do a final operation.

The code:
First, we create an instance of the class
use Eftec\MultiOne\MultiOne;
$work=MultiOne::Factory(
1000, // miliseconds
basename(__FILE__), // the file to call (itself)
4 // number of workers
);
And we want to call the next 3 functions
Create the initial works (4 workers, 4 jobs). It could be any value and not just arrays.
In this case, every worker will be assigned an array or values.
static function($numWorkers) {
$work=[
["apple","banana","pear","orange","lemon"],
["alpha","beta","gamma","delta"],
["tango","charlie","foxtrot"],
[1,2,3,4,5,6,7,8,9,10,11,12],
];
return $work; // we give every worker a work
}
Second, we do the work. In this case, we pop an element at the end of the array and we display it. If the array is empty, then we send an “end” command (we stop the worker)
static function($idWorker, $payload) {
usleep(500);
$current=array_pop($payload); // apple, banana, etc.
if($current===null) {
return MultiOne::msgAnswer('end',$payload,$current.' work done');
}
return MultiOne::msgAnswer('run',$payload,$current);
}
And third, when every worker has stopped, we could do a final operation. In this case, we are only returning a simple message.
static function($payloads) {
return 'work done';
}
We add those 3 functions as follows
$work->setMethods(
static function($numWorkers) {
$work=[
["apple","banana","pear","orange","lemon"],
["alpha","beta","gamma","delta"],
["tango","charlie","foxtrot"],
[1,2,3,4,5,6,7,8,9,10,11,12],
];
return $work; // we give every worker a work
},
static function($idWorker, $payload) {
usleep(500);
$current=array_pop($payload);
if($current===null) {
return MultiOne::msgAnswer('end',$payload,$current.' work done');
}
return MultiOne::msgAnswer('run',$payload,$current);
},
static function($payloads) {
return 'work done';
}
);
Finally, we execute the class
$work->runAuto();
Our entire code will look as follows
$work=MultiOne::Factory(
1000, // miliseconds
basename(__FILE__), // the file to call (itself)
4 // number of workers
);
$work->setMethods(
static function($numWorkers) {
$work=[
["apple","banana","pear","orange","lemon"],
["alpha","beta","gamma","delta"],
["tango","charlie","foxtrot"],
[1,2,3,4,5,6,7,8,9,10,11,12],
];
return $work; // we give every worker a work
},
static function($idWorker, $payload) {
usleep(500);
$current=array_pop($payload);
if($current===null) {
return MultiOne::msgAnswer('end',$payload,$current.' work done');
}
return MultiOne::msgAnswer('run',$payload,$current);
},
static function($payloads) {
return 'work done';
}
);
$work->runAuto();
If we call this PHP file, it will show the next values

It’s simple and it works.
A new version

Code:
$work=MultiOne::Factory(
1000, // miliseconds
basename(__FILE__), // the file to call (itself)
4 // number of workers
);
$work->setMethods(
static function($numWorkers) {
$work=[
[["apple","banana","pear","orange","lemon"],5],
[["alpha","beta","gamma","delta"],4],
[["tango","charlie","foxtrot"],3],
[[1,2,3,4,5,6,7,8,9,10,11,12],12],
];
return $work; // we give every worker a work
},
static function($idWorker, $payload) {
usleep(500);
$txt=str_repeat('📦',count($payload[0])).str_repeat('✅',$payload[1]-count($payload[0]));
$current=array_pop($payload[0]);
if($current===null) {
return MultiOne::msgAnswer('end',$payload,$txt);
}
return MultiOne::msgAnswer('run',$payload,$txt);
},
static function($payloads) {
return 'work done';
}
);
$work->runAuto();


