赞
踩
1. python threading.Thread只能使用一次start(), 否则会报RuntimeError
2. python threading.Thread无法kill,但是可以用threading.Condition()来控制线程的启动和停止
- import threading
- import time
-
- class Concur(threading.Thread):
- def __init__(self):
- super(Concur, self).__init__()
- self.iterations = 0
- self.daemon = True # Allow main to exit even if still running.
- self.paused = True # Start out paused.
- self.state = threading.Condition()
-
- def run(self):
- while True:
- with self.state: # 在该条件下操作
- plt.figure(figsize=(4, 4)) # 一些操作
- plt.ion()
- plt.axis('off') # 不需要坐标轴
- plt.imshow(self._img_qr)
- while self._pause:
- plt.pause(0.05)
- plt.ioff() # 必须和plt.ion()配合使用,如果不加ioff会出问题
-
- if self.paused:
- self.state.wait() # Block execution until notified.
-
- def resume(self): # 用来恢复/启动run
- with self.state: # 在该条件下操作
- self.paused = False
- self.state.notify() # Unblock self if waiting.
-
- def pause(self): # 用来暂停run
- with self.state: # 在该条件下操作
- self.paused = True # Block self.
'运行
该线程start之后,可以调用resume来恢复启动run(开始因为self.paused=True,所以状态进入等待暂停状态),如果想暂停线程,调用pause即可。
参考:https://stackoverflow.com/questions/15729498/how-to-start-and-stop-thread
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。