Skip to content

Instantly share code, notes, and snippets.

@mebusw
Last active August 6, 2023 15:05
Show Gist options
  • Save mebusw/b4cacbfc29cd64620a07 to your computer and use it in GitHub Desktop.
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

Simple and efficient, ty!

@yoshihikoueno
Copy link

doesn't it use processes instead of threads?

@mebusw
Copy link
Author

mebusw commented Jul 13, 2020

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

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