如何 运行 Google Cloud Function 中的子流程?

How to run a subprocess inside Google Cloud Function?

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

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。

我的云函数是由pubsub触发的。

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

我回复你了

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