我正在尝试运行一个程序,在作业完成后接受输入。我查看了几种表格,并查看了documentation。我在Debian中运行它,我知道我可以使用这个getch function来接收字符,而无需点击返回键。为了打破它,这就是我试图在我的无限循环中实现的
我也在运行线程模块来执行另一条指令。我有什么方法可以做到这一点吗?
更新:这是我到目前为止所尝试的:
首先,我尝试使用线程模块中的计时器来阻止它等待,这就是这样的。
def getchnow():
def time_up():
answer= None
print 'time up...'
wait = Timer(5,time_up) # x is amount of time in seconds
wait.start()
try:
print "enter answer below"
answer = getch()
except Exception:
print 'pass\n'
answer = None
if answer != True: # it means if variable have somthing
wait.cancel() # time_up will not execute(so, no skip)
return answer
line = getchnow()
#Add line variable to queue
#Do stuff with queue
这里的问题是它仍然在等待用户输入。
然后我尝试将getch函数放入另一个线程中。
q = Queue.Queue
q.put(getch())
if q.get() != True: # it means if variable have somthing
line = q.get()
#Add line variable to queue
#Do stuff with queue
这种尝试不允许我做任何事情。
答案 0 :(得分:0)
我更多地了解了这个link,并在底部实现了我想要的内容。
我在Linux上使用select模块进行非阻塞实现。 如果没有收到输入,则超时(此处为5秒)。 在线程中使用时特别有用,以便getch调用 非阻塞并允许线程干净地退出
# This class gets a single character input from the keyboard
class _GetchUnix:
def __init__(self):
import tty, sys
from select import select
def __call__(self):
import sys, tty, termios
from select import select
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
[i, o, e] = select([sys.stdin.fileno()], [], [], 2)
if i:
ch=sys.stdin.read(1)
else:
ch=''
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
getch = _GetchUnix()
# End Class
答案 1 :(得分:0)
我还使用了[i, o, e] = select([sys.stdin.fileno()], [], [], 2)
,但我听说它可能无法在Windows上运行。如果还有人需要一个多线程,非阻塞的输入示例:
import threading
import sys
import time
bufferLock=threading.Lock()
inputBuffer=[]
class InputThread(threading.Thread):
def run(self):
global inputBuffer
print("starting input")
while True:
line=sys.stdin.readline()
bufferLock.acquire()
inputBuffer.insert(0,line)
bufferLock.release()
input_thread=InputThread()
input_thread.start()
while True:
time.sleep(4)
bufferLock.acquire()
if len(inputBuffer)>0:
print("Popping: "+inputBuffer.pop())
bufferLock.release()