您好,欢迎来到要发发知识网。
搜索
您的当前位置:首页Python threading模块的常用方法

Python threading模块的常用方法

来源:要发发知识网

说明

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学习指路:

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


Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务