creating background processes in php for long running process

本秂侑毒 提交于 2019-12-01 21:31:39

The best (and only AFAIK) way to start a new thread thread like thing in PHP is to create a new PHP request using something like curl.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://www.yoursite.com/background-script.php');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);

curl_exec($ch);
curl_close($ch);

This will run the script, and immediately return the page. Of course there's no callback, so your best bet is to put the output of background-script.php into a database, and then ping the database every so often from the client until you see the results.

We've created a function here that returns an answer to the browser and keeps executing. We use it to send email without making the user wait for it.

public function redirectAndContinue( $url )
{
    ignore_user_abort(true);
    header("Location: $url");
    header("Connection: close");
    header("Content-Length: 0");
    flush();
}

You may use it like this

// do quick basic stuff to validate your report

$this->redirectAndContinue( $url )

// send your email or do other slow stuff

The problem is: it doesn't work with AJAX, it must be a regular page request (possibly with a POST). But you can make the progress bar anyway: save something in your database or session and then keep calling some URL to get the progress via AJAX.

As far as I remember, shell_exec or exec will block the execution of your PHP script until the process will terminate. A solution is to append a "&" at the end of your command, your script/program will be backgrounded.

"/usr/report.sh &"

I have used something like that to run external tool from PHP, but passing by an intermediary script to control PID, number of instances... Can't say if it is working under Windows host.

May I suggest Gearman? You can submit jobs to a gearman server and request the status.

Gearman provides a distributed application framework for work with multiple machines or processes. It allows applications to complete tasks in parallel, to load balance processing and to call functions between languages. The framework can be used in a variety of applications. Gearman is multi-threaded and is known to be able to carry out 50 thousand jobs per-second.

A quick tutorial is here: http://www.sitepoint.com/introduction-gearman-multi-tasking-php/

Its also quite easy to get started, and all your code will be in PHP.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!