Answered by : real-rhinoceros-nfg7mchacjwo
import threading
def worker(argument): print(argument) return
for i in range(5): t = threading.Thread(target=worker, args=[i]) t.start()
Source : | Last Update : Sat, 12 Jun 21
Answered by : weary-wolverine-1lvv5evqa6l8
import threading
import time
def thread_function(name): print(f"Thread {name}: starting") time.sleep(2) print(f"Thread {name}: finishing")
my_thread = threading.Thread(target=thread_function, args=(1,))
my_thread.start()
time.sleep(1)
my_second_thread = threading.Thread(target=thread_function, args=(2,))
my_second_thread.start()
my_second_thread.join() # Wait until thread finishes to exit
Source : | Last Update : Thu, 22 Oct 20
Answered by : clever-capybara-68arayx99ll7
def myFunction(x, y): pass
x = threading.Thread(target=myFunction, args=(x, y))
x.start()
Source : | Last Update : Wed, 27 Jan 21
Answered by : uninterested-unicorn-rchkp5piiyqh
import threading
def work(): print("Hello User")
if __name__ == "__main__": thread = threading.Thread(target=work, name='thread-a') print("How are you?") thread.join()
Source : | Last Update : Sat, 02 Jul 22
Answered by : ugly-unicorn-z7sdj238fjkl
x = threading.Thread(target=function, args=[function_args])
x.start()
Source : | Last Update : Thu, 11 Aug 22
Answered by : tense-tarantula-axiqu2327bll
# Python program to illustrate the concept
# of threading
# importing the threading module
import threading
def print_cube(num):
"""
function to print cube of given num
"""
print("Cube: {}".format(num * num * num))
def print_square(num):
"""
function to print square of given num
"""
print("Square: {}".format(num * num))
if __name__ == "__main__":
# creating thread
t1 = threading.Thread(target=print_square, args=(10,))
t2 = threading.Thread(target=print_cube, args=(10,))
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# wait until thread 1 is completely executed
t1.join()
# wait until thread 2 is completely executed
t2.join()
# both threads completely executed
print("Done!")
Source : https://www.geeksforgeeks.org/multithreading-python-set-1/ | Last Update : Mon, 18 Apr 22
Answered by : basil-vlachakis
#Python multithreading example to print current date.
#1. Define a subclass using threading.Thread class.
#2. Instantiate the subclass and trigger the thread.
import threading
import datetime
class myThread (threading.Thread): def __init__(self, name, counter): threading.Thread.__init__(self) self.threadID = counter self.name = name self.counter = counter def run(self): print("\nStarting " + self.name) print_date(self.name, self.counter) print("Exiting " + self.name)
def print_date(threadName, counter): datefields = [] today = datetime.date.today() datefields.append(today) print("{}[{}]: {}".format( threadName, counter, datefields[0] ))
# Create new threads
thread1 = myThread("Thread", 1)
thread2 = myThread("Thread", 2)
# Start new Threads
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("\nExiting the Program!!!")
Source : https://www.techbeamers.com/python-multithreading-concepts/ | Last Update : Tue, 08 Feb 22
Answered by : pleasant-penguin-a1ck7gcnz0xs
new_thread = Thread(target=fn,args=args_tuple)
Code language: Python (python)
Source : https://www.pythontutorial.net/python-concurrency/python-threading/ | Last Update : Thu, 04 Aug 22