Skip to content

Instantly share code, notes, and snippets.

@petzel
Forked from blaylockbk/multipro_template.py
Created April 13, 2021 09:51
Show Gist options
  • Save petzel/e40c63088ba1a5609708736dd6f39ea4 to your computer and use it in GitHub Desktop.
Save petzel/e40c63088ba1a5609708736dd6f39ea4 to your computer and use it in GitHub Desktop.
Template for Python multiprocessing and multithreading
import multiprocessing #:)
def do_this(number):
print number
return number*2
# Create a list to iterate over.
# (Note: Multiprocessing only accepts one item at a time)
some_list = range(0,10)
# Multiprocessing :)
num_proc = multiprocessing.cpu_count() # use all processors
num_proc = 6 # specify number to use (to be nice)
p = multiprocessing.Pool(num_proc)
result = p.map(do_this, some_list)
p.close()
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment