问题
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?
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
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)));
?>
回答1:
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();
来源:https://stackoverflow.com/questions/65438904/python-subprocess-popen-does-not-execute-php-script