Answers for "multiple threads in python"

2

python running multiple threads at the same time

from threading import Thread
from time import sleep
# use Thread to run def in background
# Example:
def func1():
    while True:
        sleep(1)
        print("Working")

def func2():
    while True:
        sleep(2)
        print("Working2")

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()
Posted by: Guest on March-02-2021
0

how to multi thread python

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
Posted by: Guest on July-10-2021

Python Answers by Framework

Browse Popular Code Answers by Language