graph BT;
id1((K)) -----> id2((2));
id1((K)) -----> id3((3));
id1((K)) -----> id4((4));
id1((K)) -----> id5((5));
graph BT;
id1((K)) -----> id4((1));
id2((K)) -----> id4((1));
id3((K)) -----> id4((1));
id1((K)) -----> id5((2));
id2((K)) -----> id5((2));
id3((K)) -----> id5((2));
id1((K)) -----> id6((3));
id2((K)) -----> id6((3));
id3((K)) -----> id6((3));
id1((K)) -----> id7((4));
id2((K)) -----> id7((4));
id3((K)) -----> id7((4));
graph BT;
id1((K)) -----> id2((1));
id3((K)) -----> id4((1));
id5((K)) -----> id6((1));
id7((K)) -----> id8((1));
_thread
: each thread is a functionthreading
: each thread is an
objectthreading
module
_thread
is considered to be
deprecatedThread
class
is used
from theading import Thread
Thread
classtarget
argumentstart
functionfrom threading import Thread
def hello():
print("Hello 5062CEM!")
threadExample1 = Thread(target=hello)
threadExample1.start() -> Hello 5062CEM!
Thread
classtarget
argumentrough in the
args` argument
start
functionfrom threading import Thread
def hello(name):
print(f"Hello {name}, and welcome to 5062CEM!")
threadExample1 = Thread(target=hello, args=(['Ian Cornelius']))
threadExample1.start() -> Hello Ian Cornelius, and welcome to 5062CEM!
Thread
classtarget
argumentargs
argument
start
functionfrom threading import Thread
def hello(name, module):
print(f"Hello {name}, and welcome to {module}!")
threadExample1 = Thread(target=hello, args=['Ian Cornelius', '5062CEM'])
threadExample1.start() -> Hello Ian Cornelius, and welcome to 5062CEM!
Thread
classrun
functionself
keywordself.data
variablefrom threading import Thread
class MyThread(Thread):
def __init__(self, name, module):
Thread.__init__(self)
self.name = name
self.module = module
self.data = None
def run(self):
self.data = f"Hello {self.name}, and welcome to {self.module}!"
customThreadExample1 = MyThread("Ian Cornelius ", "5062CEM")
customThreadExample2 = MyThread("Terry Richards", "5069CEM")
customThreadExample1.data -> Hello Ian Cornelius , and welcome to 5062CEM!
customThreadExample2.data -> Hello Terry Richards, and welcome to 5069CEM!
Thread
classrun
functionself
keyword
sleep
self.data
variablefrom threading import Thread
class MyThread(Thread):
def __init__(self, name, module, sleep):
Thread.__init__(self)
self.name = name
self.module = module
self.sleep = sleep
self.data = None
def run(self):
from time import sleep
sleep(self.sleep)
self.data = f"Hello {self.name}, and welcome to {self.module}!"
customThreadExample1 = MyThread("Ian Cornelius ", "5062CEM", 10)
customThreadExample2 = MyThread("Terry Richards", "5069CEM", 5)
customThreadExample1.start()
customThreadExample2.start()
customThreadExample1.data -> Hello Ian Cornelius , and welcome to 5062CEM!
customThreadExample2.data -> Hello Terry Richards, and welcome to 5069CEM!