Last active
February 6, 2021 02:10
-
-
Save eriky/5de7c7a1247513fa3c097911026d308c 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 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