python线程类改变类变量
import threading,time
class Rep2(threading.Thread):
delay = 5
def __init(self):
threading.Thread.__init__(self)
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)
a = Rep2()
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
@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