我需要在 Google Cloud Function 中通过 Python 中的 Subprocess 执行一些进程。
import subprocess
import os
def hello_world(request):
print(subprocess.call(["echo", "hello","world"]))
预期输出: Hello World
实际输出: 0
Google 函数是否会阻止子进程的执行,或者我是否需要以不同的方式接收输出
最佳答案
可以使用子进程
。如果您想返回子流程调用的输出,而不是退出代码,则必须使用 subprocess.check_output()
(并返回结果):
import subprocess
def hello_world(request):
return subprocess.check_output(["echo", "'hello world'"])
但是,如果您只是想返回一个字符串,则这是不必要的,以下内容就足够了:
def hello_world(request):
return "hello world"
关于Python 子进程在 Google Cloud Functions 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58539297/