Parallel processing in PHP - How do you do it?

后端 未结 8 2154
礼貌的吻别
礼貌的吻别 2020-12-01 00:00

I am currently trying to implement a job queue in php. The queue will then be processed as a batch job and should be able to process some jobs in parallel.

I already

8条回答
  •  孤独总比滥情好
    2020-12-01 00:37

    Here's a summary of a few options for parallel processing in PHP.

    AMP

    Checkout Amp - Asynchronous concurrency made simple - this looks to be the most mature PHP library I've seen for parallel processing.

    Peec's Process Class

    This class was posted in the comments of PHP's exec() function and provides a real simple starting point for forking new processes and keeping track of them.

    Example:

    // You may use status(), start(), and stop(). notice that start() method gets called automatically one time.
    $process = new Process('ls -al');
    
    // or if you got the pid, however here only the status() metod will work.
    $process = new Process();
    $process.setPid(my_pid);
    
    // Then you can start/stop/check status of the job.
    $process.stop();
    $process.start();
    if ($process.status()) {
        echo "The process is currently running";
    } else {
        echo "The process is not running.";
    }
    

    Other Options Compared

    There's also a great article Async processing or multitasking in PHP that explains the pros and cons of various approaches:

    • pthreads extension (see also this SitePoint article)
    • Amp\Thread Library
    • hack's async (requires running Facebook's HHVM)
    • pcntl_fork
    • popen
    • fopen/curl/fsockopen

    Doorman

    Then, there's also this simple tutorial which was wrapped up into a little library called Doorman.

    Hope these links provide a useful starting point for more research.

提交回复
热议问题