Python 子进程在 Google Cloud Functions 中不起作用

标签 python google-cloud-platform google-cloud-functions subprocess

我需要在 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/

相关文章:

google-cloud-platform - 我无法让 google cloud functions gen 2 仅处理来自 API 网关后面的授权请求

amazon-web-services - 使用 Google Cloud Pub/Sub 时如何在 AWS/SQS 中实现 "locked"功能?

Python 对于任何也公开 len() 的可迭代对象的正确类型是什么?

python - 将一年中的小数日转换为 Pandas 日期时间

python - 在 django rest 框架中使用 request.user 进行模型反序列化

node.js - 尝试在 firestore 中保存时,参数返回未定义

javascript - Firebase:函数返回未定义、预期的 Promise 或值

node.js - 更新字典中的“/”在 firebase 中引发错误

javascript - 如何在 firebase 云函数中使用 express `res.sendFile()`?

python - Pandas 列绑定(bind)(cbind)两个数据框