Skip to content

Instantly share code, notes, and snippets.

@Debilski
Created June 18, 2012 22:03
Show Gist options
  • Save Debilski/2951003 to your computer and use it in GitHub Desktop.
Save Debilski/2951003 to your computer and use it in GitHub Desktop.
import multiprocessing
import threading
import time
def f(i):
try:
print "starting f(%i)..." % i
time.sleep(i * 3 + 1)
print "stopping f(%i)..." % i
except KeyboardInterrupt:
print "received KeyboardInterrupt in f(%i)" % i
procs = []
for i in range(4):
p = multiprocessing.Process(target=lambda: f(i))
# p = threading.Thread(target=lambda: f(i))
p.start()
procs.append(p)
try:
for p in procs:
p.join()
except KeyboardInterrupt:
print "received KeyboardInterrupt in main process"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment