Skip to content

Instantly share code, notes, and snippets.

@mutsune
Created October 3, 2021 00:42
Show Gist options
  • Save mutsune/e83635d8f7e076689afe582e044e10f7 to your computer and use it in GitHub Desktop.
Save mutsune/e83635d8f7e076689afe582e044e10f7 to your computer and use it in GitHub Desktop.
multiprocess termination
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