我正在AWS lambda上使用subprocess() 使用这个层:https://github.com/lambci/git-lambda-layer
以下是代码:
import json
import os
import subprocess
def lambda_handler(event, context):
os.chdir('/tmp')
subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
subprocess.Popen(["touch word.txt"], shell=True)
word = str(subprocess.check_output(["ls"], shell=True))
return {
'statusCode': 200,
'body': json.dumps(word)
}
它返回:
Response:
{
"statusCode": 200,
"body": "\"b'word.txt\\\\n'\""
}
所以subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
上出现了一些错误
我通过subprocess.check_output(["git --version"], shell=True)
检查了git,它工作得很好
如何解决
有几个问题
首先,您需要等待
git
进程退出。要对subprocess.Popen
执行此操作,请对返回的Popen
对象调用.wait()
。但是,我建议使用subprocess.check_call()
来自动等待进程退出,并在进程返回非零退出状态时引发错误其次,没有必要指定
shell=True
,因为您没有使用任何shell扩展或内置。事实上,当使用shell=True
传递参数列表时,第一项是命令字符串,其余项是shell本身的参数,而不是命令最后,您的GitHub URL中缺少一个斜杠
请尝试以下方法:
相关问题 更多 >
编程相关推荐