开发者

Running a Python script in the background from a PHP file

I need to run a Python script in the background after b开发者_运维百科eing called from a PHP file. The PHP file should continue to run independently of the Python script (i.e. it shouldn't hang waiting for the Python script to finish processing, but should instead carry on processing itself).

The Python script takes one argument and produces no output (it merely processes some data in the background), then exits. I'm running Python 2.6, PHP 5.2.6, and Ubuntu 9.04.


You could use exec() to kick off the Python interperator and have it send its output to either a file or to /dev/null with redirection. Using the & operator in the exec call will cause the command to be started and PHP to continue without waiting for a result.

http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/ goes into more detail.


PHP Process Control can be used for this. The proc_open command can be used to start a process. You can later check up on it, read it's output etc. View the manual entry: http://www.php.net/manual/en/function.proc-open.php and search around google for PHP Process Control


I'm guessing the PHP file is called via Apache, in which case you won't be able to fork(). You should make your Python script daemonize. Check out python-daemon.


You could use:

<?php
shell_exec('./test.sh &');
?>

where ./test.sh should be the execution line to your script

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜