Skip to content

Instantly share code, notes, and snippets.

@mebusw
Last active August 6, 2023 15:05
Show Gist options
  • Select an option

  • Save mebusw/b4cacbfc29cd64620a07 to your computer and use it in GitHub Desktop.

Select an option

Save mebusw/b4cacbfc29cd64620a07 to your computer and use it in GitHub Desktop.
python multi-threading with map()
from multiprocessing import Pool
import time
def func(i):
time.sleep(1)
print i
pool = Pool(3)
pool.map(func, range(30))
pool.close()
pool.join()
# https://medium.com/building-things-on-the-internet/40e9b2b36148#66bf-f06f781cb52b
# http://segmentfault.com/a/1190000000414339
@iklobato

Copy link
Copy Markdown

Simple and efficient, ty!

@yoshihikoueno

Copy link
Copy Markdown

doesn't it use processes instead of threads?

@mebusw

mebusw commented Jul 13, 2020

Copy link
Copy Markdown
Author

doesn't it use processes instead of threads?

from multiprocessing import Pool as ProcessPool   ### this uses processes
from multiprocessing.dummy import Pool as ThreadPool  ### this uses threads

@mritunjaymusale

Copy link
Copy Markdown
import os 

cpuCount = os.cpu_count()

can you add cpuCount as argument to Pool() so that it automatically sets the maximum number of threads the system can handle at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment