首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >创建函数/类的线程

创建函数/类的线程

作者头像
py3study
发布于 2020-01-17 14:09:44
发布于 2020-01-17 14:09:44
1.6K0
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
# 创建类的线程
import threading
import time


class MyThread(threading.Thread):
    def run(self):
        for i in range(3):
            time.sleep(1)
            msg = "我是[线程]" + self.name + '@' + str(i)
            print(msg)


if __name__ == "__main__":
    t = MyThread()
    t.start()  # run会被自动调用,可在run中调用其他自定义类方法
代码语言:javascript
复制
# 创建函数的线程
import  threading
import time


def func1():
    for i in range(5):
        time.sleep(1)
        print("func1---%d" % i)


def func2():
    for i in range(10):
        time.sleep(1)
        print("func2---%d" % i)


def main():
    t1 = threading.Thread(target=func1)
    t2 = threading.Thread(target=func2)
    
    while True:
        # 查看当前有多少线程
        print(threading.enumerate())
        time.sleep(1)


if __name__ == "__main__":
    main()    
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/05/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档