After tinkering/testing stuffs for a few days with pthreads I manage to write a script from scratch that actually works for my needs. The point of my script is to run async like 500-1000 threads and keep creating new ones immediately when one is available. However because I'm not an expert in PHP my code may have some flaws or it could be the same task that I want but in another way, in a better way.
Some flaws that I see untill now are:
- The script could cause the CPU to go in 100% use because if there is no sleep used in while loop. See sleep(3) line.
- There are flaws in the code or unecessary stuffs. Because I'm noob with pthreads, it may be another way to do the same thing but much more optimised'.
Any suggestion to improve my code is appreciated. The reason why I post this is because I actually didn't find nothing on search engines that is doing this. All the examples are syncronised and all async examples like, run 500 threads and then stop and I was looking to something that it makes 500 threads but keep making another 500 untill reaches end of a file for example.
Script:
<?php
// Maximum number of threads.
$max_threads = 15; // I do plan to use like 500-1000 threads.
// Initiate the threads.
$threads = [];
for($i = 0; $i <= $max_threads; $i++) {
$threads[$i] = new MultiThreads($i);
$threads[$i]->start();
}
// Keep creating threads. Here I plan to make it to read a large file of urls(targets) but for demo/testing purpose I made a while loop.
while(true) {
for($i = 0; $i <= $max_threads; $i++) {
if($threads[$i]->isRunning() != true) { // If is not true(which means thread is busy) keep creating new threads. Why not creating a thread if is available.
$threads[$i] = new MultiThreads($i);
$threads[$i]->start();
}
}
//echo 'Sleeping for a while untill some threads are free.' . "\n"; // Or comment sleep(3) to not wait at all.
//sleep(3);
}
// End of script.
echo 'END! But will never end cause of while loop.' . "\n";
//////////////////////////////////////////////////////////////////
class MultiThreads extends Thread {
private $threadId;
public function __construct(int $id) {
$this->threadId = $id;
}
public function run() {
echo 'Thread(' . $this->threadId . ') started.' . "\n";
sleep(rand(1, 10)); // Simulate some 'work'.
echo 'Thread(' . $this->threadId . ') ended.' . "\n";
}
}
?>