I am using subprocess.call
in order to execute another python file. Considering that the script that will be called will never terminate the execution as it's inside an infinite loop, how can I make it possible for the original script to continue the execution after the subprocess call ?
Example: I have script1.py which does some calculations and then calls script2.py using subprocess.call(["python", "script2.py"])
, since it's inside an infinite loop the script1 gets stuck on execution, is there another way to run the file other than using subprocess module ?
subprocess.call(["python", "script2.py"])
waits for the sub-process to finish.
Just use Popen
instead:
proc = subprocess.Popen(["python", "script2.py"])
You can later do proc.poll()
to see whether it is finished or not, or proc.wait()
to wait for it to finish (as call
does), or just forget about it and do other things instead.
BTW, you might want to ensure that the same python
is called, and that the OS can find it, by using sys.executable
instead of just "python"
:
subprocess.Popen([sys.executable, "script2.py"])
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.