PHP函数只运行一次,PHP exec()函数只运行极短的Python脚本

通常,php exec函数应该阻塞,直到您运行的命令完成为止。一、 例如,PHP脚本将停止,等待命令完成,直到继续执行脚本的其余部分。我有一半的想法认为您的服务器正在经历一个max_execution_time超时,但是您已经清楚地指出,即使是几秒钟也太长,甚至这些相当短的脚本也有问题。在

我想到了几个解决办法。最简单的方法是修改python命令,以便a)将任何输出路由到文件或输出流,b)进程在后台运行。根据docs on exec:If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

我还希望您使用exec函数的两个附加可选参数。在$fileName = "foobar.docx";

$argVar = $fileName;

$cmd = "python3 /var/www/html/jan8/alexandrina.py /var/www/html/jan8/$argVar";

// modify your command to toss output, background the process, and output the process id

$cmd_modified = $cmd . " >/dev/null & echo \$!";

$cmd_output = NULL; // this will be an array of output

$cmd_return_value = NULL; // this will be the return value of the script

exec($cmd_modified, $cmd_output, $cmd_return_value);

echo "exec has completed";

echo "output:
" . print_r($cmd_output, TRUE) . "
";

echo "return value: " . print_r($cmd_return_value, TRUE);

这可能有帮助,也可能没有帮助。如果没有,我们仍然可以使用posix命令来解决这个问题。在

编辑:根据crispytx,长脚本导致$cmd_return_val为1,这意味着发生了错误。试着改变这一行:

^{pr2}$

为了这个

^{3}$

让我们知道$cmd_output的输出是什么,它至少应该有新生成的进程的进程id。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值