前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【说站】Python threading模块的常用方法

【说站】Python threading模块的常用方法

作者头像
很酷的站长
发布2022-11-23 17:06:25
1760
发布2022-11-23 17:06:25
举报
文章被收录于专栏:站长的编程笔记

Python threading模块的常用方法

说明

1、threading.curentthread():返回当前线程变量。

2、threading.enumerate():返回包含正在运行的线程的list。指线程启动后和结束前不包括启动前和结束后的线程。

threading.activeCount():与len(threading.enumerate()有相同的结果。

实例

代码语言:javascript
复制
"""
python threading模块的常用方法
"""
import time
import threading
 
 
def test1():
    print('------test1-------')
    time.sleep(3)
 
 
def test2():
    print('------test2-------')
    time.sleep(3)
 
 
def main():
 
    t1 = threading.Thread(target=test1)
    t2 = threading.Thread(target=test2)
 
    t1.start()
    t2.start()
 
    print('activeCount: %d' % threading.activeCount())
    print(threading.enumerate())
 
    while threading.activeCount() != 1:
            time.sleep(1)
            print(threading.enumerate())
 
    print(threading.enumerate())
 
 
if __name__ == '__main__':
    main()

以上就是Python threading模块的常用方法,希望对大家有所帮助。更多Python学习指路:python基础教程

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python threading模块的常用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档