Python subprocess not working in Google Cloud Functions

Viewed 493

I need to get a few processes executed via Subprocess in Python in Google Cloud Function.

import subprocess
import os
def hello_world(request):
    print(subprocess.call(["echo", "hello","world"]))

Expected output: hello world

Actual output: 0

Does google functions block the execution of subprocess or do I need to receive the output in a different way

1 Answers

It is possible to use subprocess. If you want to return the output from your subprocess call, and not the exit code, you must use subprocess.check_output() (and return the result):

import subprocess

def hello_world(request):
    return subprocess.check_output(["echo", "'hello world'"])

However if you're just trying to return a string, this is unnecessary and the following will suffice:

def hello_world(request):
    return "hello world"
Related