Created
November 20, 2017 15:19
-
-
Save iafisher/2a5e3dcb9783e1ac4726f64d5095da50 to your computer and use it in GitHub Desktop.
Simple multiprocessing example in Python
This file contains hidden or 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
"""A simple example of multiprocessing for a CPU-bound task in Python. | |
Adapted from https://www.toptal.com/python/beginners-guide-to-concurrency-and-parallelism-in-python | |
""" | |
from multiprocessing.pool import Pool | |
import time | |
def fib(n): | |
"""A (contrived) example of a CPU-intensive function that you may want to parallelize.""" | |
if n < 3: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) | |
def fib_and_finish(n): | |
r = fib(n) | |
print(' Finished!') | |
return r | |
if __name__ == '__main__': | |
print('Doing the calculations sequentially') | |
ts = time.time() | |
for _ in range(4): | |
fib_and_finish(34) | |
te = time.time() | |
print('Took {:.3}s'.format(te - ts)) | |
print('\nDoing the calculations in parallel') | |
ts = time.time() | |
with Pool(4) as p: | |
p.map(fib_and_finish, [34, 34, 34, 34]) | |
te = time.time() | |
print('Took {:.3}s'.format(te - ts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment