Last active
July 14, 2017 15:52
-
-
Save sergiolucero/e87768fd0c003f07068cf414f443ada5 to your computer and use it in GitHub Desktop.
Python concurrency 101
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
# deploy and time on Linode, LightSail, both my HPs, fpfi.cl, etc | |
import time | |
import multiprocessing | |
from dask import delayed, compute | |
nTasks = 100 | |
def long_work(): | |
for i in range(100000): | |
n = i*i*i*i*i*i #simulated long work time | |
def square(number): | |
long_work() | |
return number * number | |
def squareme(toy): | |
toy.squareme() | |
class Toy: | |
def __init__(self, i): | |
self.i = i | |
def squareme(self): | |
long_work() | |
self.i2 = self.i**2 | |
def pool_run(): | |
t0 = time.time() | |
numbers = [i for i in range(nTasks)] | |
p = multiprocessing.Pool(5) | |
squared = p.map(square, numbers) | |
print('Sum=',sum(squared)) | |
print('POOL-RUNTIME:', round(time.time()-t0,3)) | |
def dask_run(): | |
t0 = time.time() | |
toys = [Toy(i) for i in range(nTasks)] | |
squarer = [delayed(squareme)(toy) for toy in toys] | |
compute(squarer) | |
print(sum([toy.i2 for toy in toys])) | |
print('DASK-RUNTIME:', round(time.time()-t0,3)) | |
def serial_run(): | |
t0 = time.time() | |
toys = [Toy(i) for i in range(nTasks)] | |
squarer = [toy.squareme() for toy in toys] | |
print('Sum=',sum([toy.i2 for toy in toys])) | |
print('SERIAL-RUNTIME:', round(time.time()-t0,3)) | |
if __name__ == '__main__': | |
print('CORES:', multiprocessing.cpu_count()) | |
serial_run() | |
# dask_run() | |
pool_run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment