Skip to content

Instantly share code, notes, and snippets.

@edahlgren
Created June 3, 2013 20:21
Show Gist options
  • Save edahlgren/5701049 to your computer and use it in GitHub Desktop.
Save edahlgren/5701049 to your computer and use it in GitHub Desktop.
import time
from multiprocessing import Process
"""
Here's your function/task you want to make parallel
In your case the time.sleep would be replaced with lots
of computation
"""
def computation(threadid):
print "starting thread ", threadid
time.sleep(2)
print "ending thread ", threadid
"""
You can parallelize a function by making a Process out of the function
read more: http://docs.python.org/2/library/multiprocessing.html
"""
def main():
for threadid in xrange(0,3):
p = Process(target=computation, args=(threadid,))
p.start()
"""
main() will finish (die) when all Processes have finished (died)
"""
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment