python creat thread code example
Example 1: create new thread python
from threading import Thread
from time import sleep
def threaded_function(arg):
for i in range(arg):
print("running")
sleep(1)
if __name__ == "__main__":
thread = Thread(target = threaded_function, args = (10, ))
thread.start()
thread.join()
print("thread finished...exiting")
Example 2: python thread function
x = threading.Thread(target=thread_function, args=(1,), daemon=True)
x.start()
Example 3: how to thread python
import threading, time
def worker():
"""thread worker function"""
print('Worker')
return
threads = []
for i in range(5):
t = threading.Thread(target=worker)
threads.append(t)
t.start()
print('Thread')
Example 4: python threading
def myFunction(x, y):
pass
x = threading.Thread(target=myFunction, args=(x, y))
x.start()