Created
October 3, 2021 00:42
-
-
Save mutsune/e83635d8f7e076689afe582e044e10f7 to your computer and use it in GitHub Desktop.
multiprocess termination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Process | |
import time | |
import sys | |
def f(x): | |
if x == 3: | |
sys.exit(1) | |
time.sleep(2) | |
print('hello', x) | |
def terminate_all(ps): | |
time.sleep(1) | |
# sys.exit(1) | |
for p in ps: | |
p.terminate() | |
if __name__ == '__main__': | |
ps = [Process(target=f, args=(x,)) for x in range(10)] | |
for p in ps: | |
p.start() | |
# terminate_all(ps) | |
for p in ps: | |
p.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment