PHP - parallel task runner

2020-07-28 11:21发布

问题:

I need a parallel task runner in PHP (most likely on windows - IIS7 fastcgi), which has a complete implementation hiding. Its interface should be something like this:

$taskRunner = new ParallelTaskRunner();
$taskRunner->add(function () use ($sharedResource){
    //task 1
});
$taskRunner->add(function () use ($sharedResource){
    //task 2
});
$taskRunner->run(); //runs task 1, task 2 parallel

I have done some research: I am aware of techniques with which I can run PHP code parallel - pthreads, pcntl, exec, gearman, curl multi, etc...

The questions I need answer for:

  • Is it possible to make this implementation hiding with any of these techniques? How is it possible to do that (if there is some advanced workaround in implementation details)? Is there any library which has this feature?
  • How can I extract the information about the running of the parallel tasks? For example: task1: { a(); b(); }, task2: { c(); }, how can I know that the calling order of the functions was a(); b(); c(); or a(); c(); b(); or c(); a(); b();?
  • How can I debug my tasks if something goes wrong?

update

I checked many possible solution, and I ended up with pthreads. It is much easier to use threads than processes... The only drawback that it does not support xdebug.

回答1:

Promises in PHP with pthreads

Something I wrote, but have no time to talk about ...

Promises are delicious, they make something ordinarily rather complex into something very simple.

Everyone understands the concept of a stack when we talk about Javascript; this is how animations, and lots of the other fancy stuff we do on the web, are run.

In effect, my effort to write promises for pthreads is an attempt to make threading as simple as a Javascript stack, which everyone understands.

It just so happens that this very thing suits your needs almost perfectly:

$manager = new PromiseManager();
$promise = 
    new Promise($manager, new CalculateTheMeaningOfLife());
$promise
    ->then(
        new AddTwo($promise))
    ->then(
        new PrintMeaning($promise));

$manager->shutdown();

You do have to use objects and not closures (limitation of Zend), but it is the promises pattern and it is fully async and managed.

This is PHP, so can be installed with composer ... if you like that sort of thing ...

https://github.com/krakjoe/promises