方式一、
from threading import Thread import time # 开启线程的两种方式之一、 def task(name): print('%s is run' % name) time.sleep(1) print('over') if __name__ == '__main__': p = Thread(target=task, args=('egon',)) p.start() print('主线程') # 其实线程本没有主次之分
方式二、
class MyThread(Thread): def __init__(self, name): super().__init__() self.name = name def run(self): print('%s is running' % self.name) time.sleep(1) print('%s is done' % self.name) if __name__ == '__main__': t1 = MyThread('qzk') t1.start() print('主线程')