python子进程popen不执行php脚本

2024-09-29 00:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

请注意,我们使用aws lambda使用python子流程popen函数执行php脚本。我们使用以下代码在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(); 

它显示一条消息,指示数据已成功记录,但无法执行脚本。脚本很简单。它是关于在表中插入一行。我们该怎么办

更新: 下面是我在测试lambda函数时得到的信息:

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脚本在这里

<?php
$con1 = mysqli_connect("endpoint","###","###","database");
if(mysqli_query($con1,"insert into ex_inbox(time) values ('Done')") or die("the eror ".mysqli_error($con1)));
?>

Tags: thelambda函数import脚本ifmsphp
1条回答
网友
1楼 · 发布于 2024-09-29 00:18:43

AWS lambda环境将在您定义的处理程序方法返回后立即过期,因此无法在lambda环境中直接运行后台脚本/进程。您可以在调用PHP脚本后手动等待一段时间来解决此问题:

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(); 

相关问题 更多 >