- 多线程demo
from threading import Thread
import time
class MyThread(Thread):
def __init__(self, func, *args):
Thread.__init__(self)
self.func = func
self.a = 0
self.b = args[0]
def run(self):
self.a = self.a + self.b
self.func(self.a)
def sayhello(a):
print('hello')
time.sleep(3)
print(f'num is {a}')
print('success')
if __name__ == '__main__':
s = [1, 2, 3, 4, 5, 6, 7, 8]
for i in s:
t1 = MyThread(sayhello, i)
t1.start()