• python线程类改变类变量


    python线程类改变类变量

    #!/usr/bin/python3
    import threading,time
    class Rep2(threading.Thread):
        delay = 5
        def __init(self):
            threading.Thread.__init__(self)
            #self.delay = delay
            pass
        @classmethod
        def get_delay(cls):
            print('cls_delay:',cls.delay)
    
        @classmethod
        def set_delay(cls,delay):
            cls.delay = delay
            
        def run(self):
            while True:
                time.sleep(1)
                print('run_delay:',self.delay)
    
    
    # #测试1
    a = Rep2()
    # 需要调用线程类的start()方法而不是run()方法,调用run()方法类变量delay的值不会改变
    a.start() 
    print("test1,started")
    Rep2.get_delay()
    time.sleep(5)
    Rep2.delay = 2
    Rep2.get_delay()
    time.sleep(5)
    Rep2.set_delay(222)
    Rep2.get_delay()
    '''
    运行结果1:
    test1,started
    cls_delay: 5
    run_delay: 5
    run_delay: 5
    run_delay: 5
    run_delay: 5
    run_delay: 5
    cls_delay: 2
    run_delay: 2
    run_delay: 2
    run_delay: 2
    run_delay: 2
    cls_delay: 222
    run_delay: 222
    run_delay: 222
    run_delay: 222
    '''
    
    • 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
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    import threading,time
    class Rep2():
        delay = 5
        def __init(self):
            pass
            #self.delay = delay
        @classmethod
        def get_delay(cls):
            print('cls_delay:',cls.delay)
    
        @classmethod
        def set_delay(cls,delay):
            cls.delay = delay
    
        def run(self):
            print('run_delay:',self.delay)
    a = Rep2()
    a.run()
    print("test2,started")
    Rep2.get_delay()
    time.sleep(5)
    Rep2.delay = 2
    Rep2.get_delay()
    time.sleep(5)
    Rep2.set_delay(222)
    Rep2.get_delay()
    a.run()
    '''
    run_delay: 5
    test2,started
    cls_delay: 5
    cls_delay: 2
    cls_delay: 222
    run_delay: 222
    '''
    
    • 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
  • 相关阅读:
    Mybatis-Plugs开发手册
    趣解设计模式之《小王的学习秘籍》
    pdf 转 word
    Linux程序的加载过程
    【SemiDrive源码分析】【驱动BringUp】42 - Mailbox Demo实现
    http2分片流内容整合呈现方法
    苹果 M1 引入 Linux 的发展历程以及最新进展
    UrlBasedCorsConfigurationSource无法转换为CorsConfigurationSource的原因
    Docker搭建Redis集群
    Linux常用命令——builtin命令
  • 原文地址:https://blog.csdn.net/qq_41033254/article/details/128144297