Python-AWS Lambda不等待子流程完成

时间:2018-07-20 21:46:29

标签: python amazon-web-services scrapy aws-lambda

我正在尝试在AWS lambda上执行以下代码:

from os import listdir
from subprocess import call
from datetime import datetime

def lambda_handler(event, context):
    print(datetime.now())
    call('sh scp.sh', shell=True)
    print(listdir('/tmp/stores'))
    print(datetime.now())

scp.sh脚本从其他服务器执行复制文件,但是问题是主线程没有等待subprocess完成。

这是输出:

START RequestId: 6241a143-8c5f-11e8-9d42-d330bf352385 Version: $LATEST
2018-07-20 20:56:33.568496
[Errno 2] No such file or directory: '/tmp/stores': FileNotFoundError
Traceback (most recent call last):
  File "/var/task/main.py", line 8, in lambda_handler
    print(listdir('/tmp/stores'))
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/stores'

这是scp.sh文件的来源:

ssh-add nextcloud-key.pem;
scp -r ubuntu@34.218.106.211:/tmp/stores /tmp/stores;

为什么会这样?

1 个答案:

答案 0 :(得分:0)

发生的事情是scp命令失败,并且没有任何内容复制到/ tmp。代替subprocess.call,使用subprocess.check_call,您会看到它引发了异常。