I use a script to run two script. the code like this:
import subprocess
subprocess.Popen("python a.py", shell=True)
subprocess.Popen("python b.py", shell=True)
but if I end the main script, the sub process of a.py and b.py are still runing. How to solve it?
added:
the a.py and b.py is two server scripts. When I end the main script using ctrl+c,and the two servers is not end up.So how to end the all process when I end the main script?
By killing the processes.
import subprocess
p1 = subprocess.Popen("python a.py", shell=True)
p2 = subprocess.Popen("python b.py", shell=True)
# ... do things ...
p1.kill()
p2.kill()
You can also automate this with the atexit module:
import subprocess
import atexit
p1 = subprocess.Popen("python a.py", shell=True)
p2 = subprocess.Popen("python b.py", shell=True)
atexit.register(p1.kill) # register for killing
atexit.register(p2.kill)
# ... do things ...
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.