Created
February 1, 2020 07:09
-
-
Save TheProjectsGuy/5c38a77ae4be4d716116cff8ad9d75d0 to your computer and use it in GitHub Desktop.
Multithreading in python using "start_new_thread" from "_thread" library in Python 3.x
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
#!/usr/bin/env python3 | |
from _thread import start_new_thread | |
def count_to_n(id, n): | |
""" | |
Count numbers from 0 to n - 1 | |
Parameters: | |
id: The identifier for the function | |
n: The number to count upto (exclusive of n) | |
""" | |
print("%s will count to %d" % (id, n)) | |
for i in range(n): | |
print("%s at %d / %d" % (id, i, n)) | |
print("%s ended" % id) | |
if __name__ == "__main__": | |
start_new_thread(count_to_n, ("Func-1", 10)) | |
start_new_thread(count_to_n, ("Func-2", 20)) | |
# Hold execution until Ctrl + C is pressed | |
try: | |
while 1: | |
pass | |
except KeyboardInterrupt: | |
print("\b\bEnding program") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment