Is it possible to save or store the return value of a subprocess.Popen() to a global variable? For example,
global_var = none
...
def some_function():
p = subprocess.Popen(...,stdout=subprocess.PIPE, shell=True)
global_var = p
def some_function_2():
x = global_var.stdout
Something like this. I'm essentially trying to read output from a subprocess that is started earlier in code but I need to begin the read later on.
So this ended up being a silly oversight. All I needed to do was call global and the global variable name inside the function to set its value correctly like so:
global_var = none
...
def some_function():
p = subprocess.Popen(...,stdout=subprocess.PIPE, shell=True)
global global_var
global_var = p
def some_function_2():
x = global_var.stdout
You need to add one line to some_function
. I also give a better solution.
global_var = None
...
def some_function():
global global_var # without this line, Python will create a local named global_variable
p = subprocess.Popen(...,stdout=subprocess.PIPE, shell=True)
global_var = p
def some_function_2():
x = global_var.stdout
Better is:
def some_function():
# I assume this function does some other things, otherwise
# you don't need to write a function to just call another.
p = subprocess.Popen(...,stdout=subprocess.PIPE, shell=
return p
my_process = some_function()
def some_function_2():
x = my_process.stdout
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.