Skip to content

Instantly share code, notes, and snippets.

@FrankMN
Created September 2, 2016 17:33
Show Gist options
  • Save FrankMN/5494de5ae571faf181276dbf21d39a8e to your computer and use it in GitHub Desktop.
Save FrankMN/5494de5ae571faf181276dbf21d39a8e to your computer and use it in GitHub Desktop.
python multiprocessing with some sub-process doesn't exit properly
import time
from multiprocessing import Pool, TimeoutError
import multiprocessing
def run(input):
name = multiprocessing.current_process().name
print name, 'start'
if input == 1000000-1:
# this try to emulate one process doesn't end properly
time.sleep(100)
return input ** 2
print name, 'end'
if __name__ == '__main__':
p = Pool(processes=4)
nums = range(1000000)
name = multiprocessing.current_process().name
print "main process", name, 'start'
start = time.time()
res = p.map_async(run, nums)
try:
print res.get(timeout=5)
except TimeoutError as te:
print te.message
end = time.time()
print 'main process', name, 'end'
print 'multi-processes runs %0.2f seconds.' % ((end - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment