Skip to content

Instantly share code, notes, and snippets.

@Benoss
Created January 6, 2014 03:05
Show Gist options
  • Save Benoss/8277602 to your computer and use it in GitHub Desktop.
Save Benoss/8277602 to your computer and use it in GitHub Desktop.
Python pool multi thread/process example
import time, random
PROCESS = True
JOB_POOL_SIZE = 10
DATABASE_POOL_SIZE = 5
def apply_job(arg):
r = random.random() *10
time.sleep(r)
return (arg, r)
def db_callback(arg):
print "DbCallback", arg
def update_db(arg):
time.sleep(1)
return arg
if __name__ == '__main__':
print "Creating Pool"
if PROCESS:
import multiprocessing as mult
pool = mult.Pool(processes=JOB_POOL_SIZE)
database_pool = mult.Pool(processes=DATABASE_POOL_SIZE)
else:
from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=JOB_POOL_SIZE)
database_pool = ThreadPool(processes=DATABASE_POOL_SIZE)
jobs= range(1, 50)
for res in pool.imap_unordered(apply_job, jobs):
#for res in pool.imap(apply_job, jobs): if you need the result to be ordered for whatever reason
database_pool.apply_async(update_db, args=[res], callback=db_callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment