• 技术文章 >Python技术 >Python基础教程

    Python threading模块的常用方法

    小妮浅浅小妮浅浅2021-07-21 09:30:50原创3261

    说明

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

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

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

    实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    """

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


    专题推荐:python threading
    上一篇:python列表如何传递到线程? 下一篇:Python如何自定义类继承threading.Thread

    相关文章推荐

    • python中figure()函数画两张图• python中subplot函数怎么画图?• python局部作用域是什么• python异常时的语句处理• python中random模块求随机数

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网