ITPub博客

首页 > IT职业 > IT职场 > Python如何自定义类继承threading.Thread

Python如何自定义类继承threading.Thread

IT职场 作者:zuike2000 时间:2021-09-11 09:54:31 0 删除 编辑

说明

1、使用threading模块可以完成多任务的程序开发。

2、为了使每个线程的封装更加完美,在使用threading模块时,通常会定义一个新的子类class,只需继承threading.Thread即可,然后重写run方法。

实例

"""
Python多线程的使用
"""
import time
import threading
 
 
class MyThread(threading.Thread):
    
    # def __init__(self):
    #    super().__init__()
    
    def run(self):
        for i in range(3):
            time.sleep(1)
            msg = "I'm "+self.name+' @ '+str(i) #name属性中保存的是当前线程的名字
            print(msg)
 
            
def main():
    t = MyThread()
    t.start()
    
if __name__ == '__main__':
main()

以上就是Python自定义类继承threading.Thread的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2203/viewspace-2829810/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 爬虫工具有哪些
请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    404
  • 访问量
    2062