Last active
December 9, 2018 14:17
-
-
Save aashish-chaubey/71c45abaa8a742ce198afaecf5c23999 to your computer and use it in GitHub Desktop.
Creating threads in python using threading module
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
from threading import Thread | |
import time | |
def job(name): | |
# Create a process heavy job | |
time.sleep(2) | |
print("The name is: %s" % name) | |
def main(): | |
print("Program starts!") | |
names = ['Mark', 'Tyler', 'Micheal', 'Kevin', 'Keiron'] | |
start = time.time() | |
for name in names: | |
# A new thread is created for each of the name print job | |
worker = Thread(target=job,args=(name, )) | |
worker.start() | |
# job(name) | |
# Wait for all the thread to complete and join into the main thread | |
worker.join() | |
print("Time for the execution is: %s" % (time.time() - start)) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment