睡眠不中断程序
回答
没有比为你准备的0计时器线程使自己的线程更简单的方法:“世界城市论坛,世界城市论坛”
import threading
timer = None
def wuf():
global timer
print "Wuf-wuf!"
timer = threading.Timer(5, wuf)
timer.start()
timer = threading.Timer(5, wuf)
timer.start()
input() # Don't exit the program
该代码将等待5秒钟,然后开始打印每5秒钟一次。
如果你想从主线程阻止它这样做:
timer.cancel()
但如果您使用的是事件驱动的GUI系统,如wxPython的或PyQt的编写GUI应用程序,那么你应该使用他们的事件管理计时器。特别是如果你正在从定时器回调改变一些GUI状态。
编辑: 哦,没事,这里是你的完整的答案:
import threading
seconds = 1 # Initial time must be the time+1 (now 0+1)
timer = None
def tick():
global seconds, timer
seconds -= 1
if seconds==0:
print("%i seconds left" % seconds)
print("Timer expired!")
return
# printing here will mess up your stdout in conjunction with input()
print("%i second(s) left" % seconds)
timer = threading.Timer(1, tick)
timer.start()
seconds += int(input("Initial countdown interval: "))
tick()
while 1:
seconds += int(input("Add: "))
if not timer.is_alive():
print("Restarting the timer!")
seconds += 1
tick()
或Easy版本螺纹(但有点clumsyer然后使用threading.Thread):
from thread import start_new_thread as thread
from time import sleep
seconds = 1 # Initial time+1
alive = 0
def _tick():
global seconds, alive
try:
alive = 1
while 1:
seconds -= 1
if seconds==0:
print("%i seconds left" % seconds)
print("Timer expired!")
alive = 0
return
# printing here will mess up your stdout in conjunction with input()
print("%i second(s) left" % seconds)
sleep(1)
except: alive = 0
def tick():
thread(_tick,())
# Then same as above:
seconds += int(input("Initial countdown interval: "))
tick()
while 1:
seconds += int(input("Add: "))
if not alive:
print("Restarting the timer!")
seconds += 1
tick()
你必须认识到,在线程内使用stdout将在input()输出的提示消息之后插入打印的文本。
这会令人困惑。如果你想避免这一点,那么你将不得不编写另一个线程来从队列中获取消息并输出它们。
如果最后一条消息是提示消息,则必须将其从屏幕上移除,写入新消息,然后返回提示消息,并相应地定位光标。
你可以通过在线程的子类中实现类文件接口来实现。线程,然后用它替换sys.stdout。也许重写input()以指示何时提示消息不存在,并读取stdin。
您将需要使用线程来完成此操作。你所要做的就是创建threading.Thread
的子类,覆盖run()
方法,并在该线程上添加一些外部控制方法。
下面是一个简单的例子,您可以尝试根据自己的口味进行调整。
import threading
import time
class SleepingThread(threading.Thread):
def __init__(self, sleep_for):
super(SleepingThread, self).__init__()
self.sleep_for = sleep_for
def run(self):
while self.sleep_for != 0:
time.sleep(1)
self.sleep_for -= 1
print("Done sleeping")
def add_seconds(self, seconds):
self.sleep_for += seconds
def get_sleep_for(self):
return self.sleep_for
sleeping_thread = SleepingThread(10)
sleeping_thread.start()
while True:
print(sleeping_thread.get_sleep_for())
sleeping_thread.add_seconds(int(input('add > ')))
,如果你想要去认真对待它,不要忘了join()
。
这不完全是我想要的。它仍然等待输入,并且我希望它在每次输入减少时都打印新的时间,而不是在输入之后。 – lol
- 1. 睡眠时线程中断
- 2. 中断睡眠线程
- 3. Perl的线程中断睡眠不行
- 4. 中断或停止睡眠线程
- 5. 主题 - 睡眠和中断
- 6. 阻塞或睡眠中断处理程序
- 7. 睡眠()或睡眠()的准确程度
- 8. Qt中的可中断睡眠?
- 9. Java的睡眠所中断异常
- 10. Time1中断唤醒单片机睡眠
- 11. 线程中睡眠问题
- 12. Andengine睡眠线程?
- 13. Openmp线程睡眠
- 14. 当应用程序运行时睡眠在循环中,但睡眠太少
- 15. 睡在睡眠中的NSTimer
- 16. 睡眠内线程没有睡眠外线程 - Java
- 17. iPhone Web应用程序从睡眠中醒来后,JavaScript事件中断
- 18. C#程序 - 暂停或睡眠完成
- 19. 在C程序中执行睡眠程序几秒钟
- 20. Python睡眠函数不捕获中断信号
- 21. 睡眠功能不会在序列
- 22. Java中的睡眠线程或进程
- 23. RxJava调度程序不会改变线程与睡眠
- 24. 睡眠和睡眠开始错误
- 25. PHP睡眠VS庆典睡眠
- 26. 如何禁用睡眠/睡眠功能?
- 27. Python睡眠过程进入无限期睡眠
- 28. 睡眠()在Android中
- 29. Syncronous睡眠到ASYNCIO协程
- 30. 使所有线程睡眠
你绝对需要穿线。 –
您称为可变时间和使用时间模块。 2个相同的名字。 – Dalen
哎呦!!!!!!!!!!! – lol