Reputation: 21
I need to get information from a bash command which takes several seconds. I want the rest of program to continue until I get the returncode. I tried to do it with multiprocessing but I cant get the returncode of the subprocess alltough the console prints the correct returncode.
from multiprocessing import Process, Value
import subprocess
num = Value("d", 0.0)
class foo(object):
def __init__(self):
self.createProcess()
def createProcess(self):
p = Process(target=self.Process, args=(num,))
p.start()
...Do Stuff here in parallel...
def Process(self,n):
somebashParam = int(n.value)
p = subprocess.Popen("some -command"+str(somebashParam),shell=True)
out, err = p.communicate()
n.value = p.returncode
Upvotes: 1
Views: 559
Reputation: 42758
External processes automatically run in parallel. If you are only interested in the return code, you don't need any additional code:
n = 23
process = subprocess.Popen(["some", "-command", str(n)])
while process.poll() is None:
do_something_else()
result = process.wait()
Upvotes: 1