0

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";
  }
}
?>
John Doe
  • 1
  • 1
  • 1
    If you code is working correctly you may want to ask for it to be reviewed over on [Code Review](https://codereview.stackexchange.com/). – Dave Aug 23 '19 at 14:44
  • I'm sorry I didn't know that there is a code review and yes my code is actually working. – John Doe Aug 23 '19 at 14:49
  • Not a problem at all. A lot of the same eyes will see it but that's a good place to ask about possible improvements. – Dave Aug 23 '19 at 14:50
  • I would also try and research the optimal number of threads. Not sure if 1000 threads will be good or you should stick to something based on some (lowish) multiplier of the core count. – Nigel Ren Aug 23 '19 at 15:18
  • @NigelRen I used before xargs with 1000 threads without any issue, however the reason that I will move to pthreads is because this is async, xargs use forks while multi threading and that's a waste of resources. This 'should' work much faster. – John Doe Aug 23 '19 at 15:44

0 Answers0