Skip to content

Instantly share code, notes, and snippets.

@eriky
Last active February 6, 2021 02:10
Show Gist options
  • Save eriky/f58b85c2d74981e6e3651d557643a0d0 to your computer and use it in GitHub Desktop.
Save eriky/f58b85c2d74981e6e3651d557643a0d0 to your computer and use it in GitHub Desktop.
import time
import multiprocessing
# A CPU heavy calculation, just
# as an example. This can be
# anything you like
def heavy(n, myid):
for x in range(1, n):
for y in range(1, n):
x**y
print(myid, "is done")
def doit(n):
heavy(500, n)
def pooled(n):
# By default, our pool will have
# numproc slots
with multiprocessing.Pool() as pool:
pool.map(doit, range(n))
if __name__ == "__main__":
start = time.time()
pooled(80)
end = time.time()
print("Took: ", end - start)
# This takes about 23 seconds as well.
# That's half of the threaded version.
# My pc has two cores, so it's exactly
# what we would expect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment