Skip to content

Instantly share code, notes, and snippets.

@TamasNo1
Created May 11, 2019 18:40
Show Gist options
  • Select an option

  • Save TamasNo1/da5c63fd86cfe68d84811e438568bc14 to your computer and use it in GitHub Desktop.

Select an option

Save TamasNo1/da5c63fd86cfe68d84811e438568bc14 to your computer and use it in GitHub Desktop.
Python multiprocessing template
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