python子进程popen不执行php脚本 [英] python subprocess popen does not execute php script
问题描述
请注意,我们使用aws-lambda通过python子进程popen函数执行php脚本.我们使用以下代码在python popen中调用文件:
Please note, we are using aws-lambda to execute a php script using python subprocess popen function. We used the following code to call the file in python popen:
import json
import subprocess
# if the script doesn't need output.
def lambda_handler(event, context):
cmd = ["/usr/bin/php74/", "/home/admin/web/path/post.php"]
proc = subprocess.Popen(cmd, shell=True,stdout = subprocess.PIPE, stderr = subprocess.PIPE)
script_response = proc.stdout.read();
它显示一条消息,指示已成功记录数据,但无法执行脚本.脚本很简单.它是关于在表中插入一行.我们该怎么办?
It displays a message indicating that data has been successfully logged, but it fails to execute the script. The script is simple. It is about inserting a row to a table. What shall we do?
更新:这是我正在测试lambda函数的消息:
Update: Here is the message i'm getting on testing the lambda function:
Response:
null
Request ID:
"37e74321-4232-4d1f-aea8-3d5a82444de2"
Function logs:
START RequestId: 37e74321-4232-4d1f-aea8-3d5a82444de2 Version: $LATEST
END RequestId: 37e74321-4232-4d1f-aea8-3d5a82444de2
REPORT RequestId: 37e74321-4232-4d1f-aea8-3d5a82444de2 Duration: 43.42 ms Billed Duration: 44 ms Memory Size: 128 MB Max Memory Used: 53 MB Init Duration: 113.16 ms
我已验证的php脚本在这里
My verified php script goes here
<?php
$con1 = mysqli_connect("endpoint","###","###","database");
if(mysqli_query($con1,"insert into ex_inbox(time) values ('Done')") or die("the eror ".mysqli_error($con1)));
?>
推荐答案
一旦您定义的处理程序方法返回,AWS lambda环境就会过期,因此无法在lambda环境中直接运行后台脚本/进程.您可以通过在调用PHP脚本后手动等待一段时间来解决此问题:
AWS lambda environment will expire as soon as the handler method you defined returns, thus running background scripts/processes in your lambda environment directly is not possible. You can work around this issue by manually waiting for some time after you call the PHP script:
proc = subprocess.Popen(cmd, shell=True,stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# wait for your subprocess to complete
time.sleep(10)
script_response = proc.stdout.read();
这篇关于python子进程popen不执行php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!