Created
June 3, 2013 20:21
-
-
Save edahlgren/5701049 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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