Python threading模块的常用方法

2025-12-01 0 71,243

说明

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

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

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

实例

"""
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电脑。


收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 Python threading模块的常用方法 https://www.zhiup.top/1664.html

相关