说明
1、使用threading模块可以完成多任务的程序开发。
2、为了使每个线程的封装更加完美,在使用threading模块时,通常会定义一个新的子类class,只需继承threading.Thread即可,然后重写run方法。
实例
""" Python多线程的使用 """ importtime importthreading classMyThread(threading.Thread): #def__init__(self): #super().__init__() defrun(self): foriinrange(3): time.sleep(1) msg="I'm"+self.name+'@'+str(i)#name属性中保存的是当前线程的名字 print(msg) defmain(): t=MyThread() t.start() if__name__=='__main__': main()
以上就是Python自定义类继承threading.Thread的方法,希望对大家有所帮助。
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
下一篇