简体   繁体   English

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

[英]Python - AWS Lambda not waiting for subprocess finish

I'm trying to execute the following code on 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())

The scp.sh script execute a copy files from other server, but the problem is, the main thread is not waiting for the subprocess finishes. scp.sh脚本从其他服务器执行复制文件,但是问题是主线程没有等待subprocess进程完成。

This is the output: 这是输出:

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'

This is the source of scp.sh file: 这是scp.sh文件的来源:

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

Why this is happening? 为什么会这样呢?

What's happening is that the scp command is failing and nothing gets copied to /tmp. 发生的情况是scp命令失败,并且没有任何内容复制到/ tmp。 Instead of subprocess.call , use subprocess.check_call and you'll see that it raises an exception. 代替subprocess.call ,使用subprocess.check_call ,您将看到它引发了异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM