Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Created December 16, 2018 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryantuck/2a8d8a13a5260a3ef653b17f9900e160 to your computer and use it in GitHub Desktop.
Save ryantuck/2a8d8a13a5260a3ef653b17f9900e160 to your computer and use it in GitHub Desktop.
import random
import time
import multiprocessing as mp
def sleep_random(n):
sleep_seconds = random.randint(0, 3)
print(f'sleeping for {sleep_seconds} seconds')
# randomly fail
x = 5 / sleep_seconds
time.sleep(sleep_seconds)
return n
def process_stuff():
jobs = []
results = []
def log_result(x):
results.append(x)
for i in range(10):
p = mp.Process(target=sleep_random, args=(i,))
jobs.append(p)
p.start()
while True:
n_working = len([p for p in jobs if p.is_alive()])
print(f'Jobs running: {n_working} / {len(jobs)}')
if n_working == 0:
break
time.sleep(1)
print(results)
# this one seems to work nicely
def pool_stuff():
jobs = []
results = []
def log_result(x):
results.append(x)
with mp.Pool(processes=4) as pool:
for i in range(10):
jobs.append(pool.apply_async(sleep_random, args=(i,), callback=log_result))
pool.close()
pool.join()
return jobs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment