我需要通过Google Cloud Function中的Python中的子进程来执行一些进程。
import subprocess
import os
def hello_world(request):
print(subprocess.call(["echo", "hello","world"]))
预期输出:你好,世界
实际输出:0
Google函数会阻止子流程的执行还是我需要以其他方式接收输出
请记住,Cloud Functions是无服务器的计算平台,主要功能之一是没有要配置,管理,修补或更新的服务器。
应该使用python中的子进程来访问系统命令,因此,基本上,您试图从运行Cloud Function的计算机获得响应,而这是不可能的。
在您的特定情况下,使用以下代码将获得预期的输出:
def hello_world(request):
return f'Hello, world!'
可以使用subprocess
。如果要返回子过程调用的输出而不是退出代码,则必须使用subprocess.check_output()
(并返回结果):
import subprocess
def hello_world(request):
return subprocess.check_output(["echo", "'hello world'"])
但是,如果您只是尝试返回一个字符串,则这是不必要的,并且满足以下条件:
def hello_world(request):
return "hello world"