Skip to content

Instantly share code, notes, and snippets.

@tomarv2
Created January 24, 2022 20:35
Show Gist options
  • Save tomarv2/4d2f2aa70c6619051105f66a9e931496 to your computer and use it in GitHub Desktop.
Save tomarv2/4d2f2aa70c6619051105f66a9e931496 to your computer and use it in GitHub Desktop.
mutliprocessing using pool with apply_async with callback
from multiprocessing import Pool
import os
import time
list_of_movie_names = ["john_wick_1", "john_wick_1", "john_wick_3"]
def hello_world(name):
print("IT WASN'T JUST A PUPPY")
time.sleep(1)
print('YOU WANTED ME BACK...')
return f"Hello: {name}"
results = []
def response(result):
results.append(result)
def demo_multi_processing():
tic = time.time()
pool = Pool(processes=os.cpu_count())
for name in list_of_movie_names:
pool.apply_async(hello_world, args=(name,), callback=response)
pool.close()
pool.join()
print(results)
toc = time.time()
print(f'Completed in {toc - tic} seconds')
if __name__ == '__main__':
demo_multi_processing()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment