如何在 Google Cloud Function 中运行子流程? [英] How to run a subprocess inside Google Cloud Function?

本文介绍了如何在 Google Cloud Function 中运行子流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的云函数中运行一个子进程来触发我写在外部文件中的数据流管道.

I want to run a subprocess inside my cloud function to trigger my dataflow pipeline which is written in an external file.

import base64
import subprocess

def hello_pubsub(event, context):

    if 'data' in event:
        name = base64.b64decode(event['data']).decode('utf-8')

    else:
        name = 'World'
    print('Hello {}!'.format(name))
    var = subprocess.run(["python", "./defaultTrigger.py", "--input_topic", " projects/my_project/subscriptions/sub1" ,"--output_topic", "projects/my_project/topics/topic2"])

Ps:我的数据流管道从 sub1 读取并写入到 topic2.

Ps : my dataflow pipeline reads from sub1 and writes into topic2.

我的云功能是由pubsub触发的.

My cloud function is triggered by pubsub.

CompletedProcess(args=['python', '...', ... ] ,返回码=1

CompletedProcess(args=['python', '...', ... ] , returncode=1

推荐答案

我回复你了这里

另一部分是您希望在 Function 中执行子流程并在该子流程中调用python".请记住,您处于无服务器架构中,您不知道底层服务器、操作系统和平台是什么.如果调用被接受,执行子进程 python 可能会导致意外的事情(python 2 或 3?哪些依赖项?...).实际上,您需要为请求处理时间付费.如果你fork进程并运行后台线程,计费是不公平的.这就是为什么禁止进行此类操作的原因.我建议您重新设计您的应用

The other part is your wish to perform a subprocess in Function and to call "python" in this subprocess. Remember, you are in serverless architecture, you don't know what is the underlying server, OS and platform. Performing a subprocess python can lead to unexpected thing (python 2 or 3? Which dependencies?...) if the call is accepted. Indeed, you are billed for the request processing time. If you fork the process and run background thread, the billing is not fair. That's why it's forbidden to perform this kind of operation. I recommend you to redesign your app

这篇关于如何在 Google Cloud Function 中运行子流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆