Skip to content

Instantly share code, notes, and snippets.

@sandeepkumar-skb
Last active March 31, 2021 16:03
Show Gist options
  • Save sandeepkumar-skb/c716f199705c3480f6d6edb521a5336f to your computer and use it in GitHub Desktop.
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.
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