Skip to content

Instantly share code, notes, and snippets.

@eriky
Last active February 6, 2021 02:10
Show Gist options
  • Save eriky/5de7c7a1247513fa3c097911026d308c to your computer and use it in GitHub Desktop.
Save eriky/5de7c7a1247513fa3c097911026d308c to your computer and use it in GitHub Desktop.
import threading
import time
# An I/O intensive calculation.
# We simulate it with sleep.
def heavy(n, myid):
time.sleep(2)
print(myid, "is done")
def threaded(n):
threads = []
for i in range(n):
t = threading.Thread(target=heavy, args=(500,i,))
threads.append(t)
t.start()
for t in threads:
t.join()
if __name__ == "__main__":
start = time.time()
threaded(80)
end = time.time()
print("Took: ", end - start)
# This takes a little over 2s on my system
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment