码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
  • 相关阅读:
    【国产MCU】-CH32V307-SysTick中断与延时功能实现
    【Java并发编程三】线程的基本使用一
    【工具】OCR方法|不用下载额外的软件,提取扫描中英文PDF的目录文本的最优解!(一)
    公共经济学名词解释
    19、一起来学习MySQL 索引
    主流商业智能(BI)工具的比较(二):Power BI与Domo
    华为OD机试真题-求最小步数-2023年OD统一考试(B卷)
    MySQL5.7版本与8.0版本在Ubuntu(WSL环境)系统安装
    带你进入桌面数控机床金工实训室
    【毕业设计】24-基于单片机的电子显示屏的设计与应用(原理图+源码+仿真工程+论文+答辩PPT)
  • 原文地址:https://blog.csdn.net/qq_41033254/article/details/128144297
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号