Created
May 11, 2019 18:40
-
-
Save TamasNo1/da5c63fd86cfe68d84811e438568bc14 to your computer and use it in GitHub Desktop.
Python multiprocessing template
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 datetime | |
| import time | |
| import os | |
| def f(x): | |
| time.sleep(0.2) | |
| return x*x | |
| before = datetime.datetime.now() | |
| cpu_count = os.cpu_count() | |
| processes = cpu_count - 1 if cpu_count > 1 else 1 | |
| with Pool(processes=processes) as pool: | |
| # launching multiple evaluations asynchronously *may* use more processes | |
| multiple_results = [pool.apply_async(f, (i,)) for i in range(100)] | |
| results = [res.get(timeout=10) for res in multiple_results] | |
| print(results) | |
| print(datetime.datetime.now() - before) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment