Skip to content

Instantly share code, notes, and snippets.

@aashish-chaubey
Last active December 9, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aashish-chaubey/71c45abaa8a742ce198afaecf5c23999 to your computer and use it in GitHub Desktop.
Save aashish-chaubey/71c45abaa8a742ce198afaecf5c23999 to your computer and use it in GitHub Desktop.
Creating threads in python using threading module
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