Last active
January 24, 2022 20:31
-
-
Save tomarv2/3c606dd79bfcb5e3e972730f0eb8ba3d to your computer and use it in GitHub Desktop.
mutliprocessing using pool with apply_async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from multiprocessing import Pool | |
| import os | |
| import time | |
| def hello_world(name): | |
| print("IT WASN'T JUST A PUPPY") | |
| time.sleep(1) | |
| print('YOU WANTED ME BACK...') | |
| return f"Hello: {name}" | |
| list_of_movie_names = ["john_wick_1", "john_wick_1", "john_wick_3"] | |
| def demo_multi_processing(): | |
| tic = time.time() | |
| pool = Pool(processes=os.cpu_count()) | |
| res = list(pool.apply_async(hello_world, args=(name,)) for name in list_of_movie_names) | |
| pool.close() | |
| pool.join() | |
| results = [r.get() for r in res] | |
| 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