首先,我使用的是 python 2.7.5 和 Windows x64,我的应用程序针对这些参数。
在经过一定时间后,我需要一种取消 raw_input 的方法。目前我的主线程启动了两个子线程,一个是计时器(threading.Timer),另一个触发 raw_input。它们都向主线程监视的 Queue.queue 返回一个值。然后它对发送到队列的内容进行操作。
# snip...
q = Queue.queue()
# spawn user thread
user = threading.Thread(target=user_input, args=[q])
# spawn timer thread (20 minutes)
timer = threading.Timer(1200, q.put, ['y'])
# wait until we get a response from either
while q.empty():
time.sleep(1)
timer.cancel()
# stop the user input thread here if it's still going
# process the queue value
i = q.get()
if i in 'yY':
# do yes stuff here
elif i in …Run Code Online (Sandbox Code Playgroud)