Last active
March 31, 2021 16:03
-
-
Save sandeepkumar-skb/c716f199705c3480f6d6edb521a5336f to your computer and use it in GitHub Desktop.
This python example demonstrates python threading being useful for IO bound operations.
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
import threading | |
import time as time | |
def display(i): | |
print("Thread id: ", i) | |
time.sleep(4) | |
if __name__ == "__main__": | |
start = time.time() | |
for i in range(5): | |
display() | |
stop = time.time() | |
print("non threading time: ", stop-start) | |
start = time.time() | |
threads = [] | |
for i in range(5): | |
t = threading.Thread(target=display, args=(i,)) | |
t.start() | |
threads.append(t) | |
for t in threads: | |
t.join() | |
stop = time.time() | |
print("threading time: ", stop-start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment