Does PHP have threading?

前端 未结 13 1692
孤城傲影
孤城傲影 2020-11-22 09:39

I found this PECL package called threads, but there is not a release yet. And nothing is coming up on the PHP website.

13条回答
  •  失恋的感觉
    2020-11-22 10:31

    Here is an example of what Wilco suggested:

    $cmd = 'nohup nice -n 10 /usr/bin/php -c /path/to/php.ini -f /path/to/php/file.php action=generate var1_id=23 var2_id=35 gen_id=535 > /path/to/log/file.log & echo $!';
    $pid = shell_exec($cmd);
    

    Basically this executes the PHP script at the command line, but immediately returns the PID and then runs in the background. (The echo $! ensures nothing else is returned other than the PID.) This allows your PHP script to continue or quit if you want. When I have used this, I have redirected the user to another page, where every 5 to 60 seconds an AJAX call is made to check if the report is still running. (I have a table to store the gen_id and the user it's related to.) The check script runs the following:

    exec('ps ' . $pid , $processState);
    if (count($processState) < 2) {
         // less than 2 rows in the ps, therefore report is complete
    }
    

    There is a short post on this technique here: http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

提交回复
热议问题