I want to run several subprocesses using a program, i.e. the module foo.pystarts several instances bar.py.
Since I sometimes have to interrupt a process manually, I need a process identifier to execute the kill command.
Despite the fact that the whole setup is quite "dirty", is there a good pythonic way to get the process pidif the process is started through os.system?
foo.py:
import os
import time
os.system("python bar.py \"{0}\ &".format(str(argument)))
time.sleep(3)
pid = ???
os.system("kill -9 {0}".format(pid))
bar.py:
import time
print("bla")
time.sleep(10) % within this time, the process should be killed
print("blubb")
source
share