Skip to content

Python code snippet – How to multi thread ?

from Threading import Thread

def thread_fn():
	# do stuff here. 
num_threads = 10
for i in range(num_threads):
	t = Thread(target=thread_fn, args=[]) # args[] contains function arguments
    t.start()                             # thread is now running!
    threads.append(t)                     # hold onto it for it later

for t in threads:
    t.join()                              # waits for each t to finish
See also  Python code snippet - How to get index in for loop?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.