Skip to content

Instantly share code, notes, and snippets.

@emr-arvig
Last active September 23, 2019 02:18
Show Gist options
  • Select an option

  • Save emr-arvig/d74a05fa036b3785458061ec1016cb45 to your computer and use it in GitHub Desktop.

Select an option

Save emr-arvig/d74a05fa036b3785458061ec1016cb45 to your computer and use it in GitHub Desktop.
Multiprocessing callback example
from multiprocessing import Pool
RESULTS = []
CALLBACK_DATA = []
def mycallback(x):
print('mycallback is called with {}'.format(x))
CALLBACK_DATA.append(x)
def multiply_by_two(x):
return int(x) * 2
MP_POOL=Pool(processes=5)
for x in range(1, 10):
MP_APPLY = MP_POOL.apply_async(multiply_by_two, (x,), callback=mycallback)
RESULTS.append(MP_APPLY)
for MP_APPLY in RESULTS:
MP_APPLY.wait()
#pool.join()
#pool.close()
print('callback data')
print(sorted(CALLBACK_DATA))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment