简体   繁体   English

从PHP文件在后台运行Python脚本

[英]Running a Python script in the background from a PHP file

I need to run a Python script in the background after being called from a PHP file. 从PHP文件调用后,我需要在后台运行Python脚本。 The PHP file should continue to run independently of the Python script (ie it shouldn't hang waiting for the Python script to finish processing, but should instead carry on processing itself). PHP文件应继续独立于Python脚本运行(即,它不应挂起以等待Python脚本完成处理,而应自行进行处理)。

The Python script takes one argument and produces no output (it merely processes some data in the background), then exits. Python脚本采用一个参数,不产生任何输出(它仅在后台处理一些数据),然后退出。 I'm running Python 2.6, PHP 5.2.6, and Ubuntu 9.04. 我正在运行Python 2.6,PHP 5.2.6和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. 您可以使用exec()启动Python插入器,并通过重定向将其输出发送到文件或/ dev / null。 Using the & operator in the exec call will cause the command to be started and PHP to continue without waiting for a result. 在exec调用中使用&运算符将导致命令启动,PHP继续运行而无需等待结果。

http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/ goes into more detail. http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/更加详细。

PHP Process Control can be used for this. PHP Process Control可以用于此目的。 The proc_open command can be used to start a process. proc_open命令可用于启动进程。 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 您可以稍后对其进行检查,读取其输出等。查看手册条目: http//www.php.net/manual/en/function.proc-open.php并在Google周围搜索PHP Process Control

I'm guessing the PHP file is called via Apache, in which case you won't be able to fork() . 我猜想PHP文件是通过Apache调用的,在这种情况下,您将无法fork() You should make your Python script daemonize. 您应该使Python脚本守护程序。 Check out python-daemon . 查看python-daemon

You could use: 您可以使用:

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

where ./test.sh should be the execution line to your script 其中./test.sh应该是脚本的执行行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM