非阻塞式键盘输入python [英] non blocking keyboard input python
问题描述
当对等点不断侦听传入连接(新对等点)并通过终端(用户输入)向其他对等点发送命令时,我正在构建p2p系统.我一直在寻找新的同伴时,很难从键盘上寻找用户输入.
Im building a p2p system when the peers are constantly listening for incoming connections (new peers) and sending commands through the terminal (user input) to the other peers. I am having difficulty looking for user input from the keyboard while always looking for new peers.
print 'Listening...'
while not shutdown:
while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: #look for keyboard input... not working
line = sys.stdin.readline()
if line:
send_message(line)
else: # an empty line means stdin has been closed
print('eof')
exit(0)
try: # listen for other peers
clientsock,clientaddr = mySocket.accept()
print 'Incoming connection from', clientaddr
clientsock.settimeout(None)
t = threading.Thread( target = HandlePeer, args = [clientsock] )
t.start()
except KeyboardInterrupt:
print "shutting down"
shutdown = True
continue
except Exception,e:
print 'error in peer connection %s %s' % (Exception,e)
mySocket.close()
HandlePeer检查来自新连接的对等方的传入消息.我只需要一种发送消息的方式.
HandlePeer checks for incoming messages from the newly connected peer. I just need a way of sending messages.
推荐答案
简短的答案是,您需要使用诅咒.
The short answer is you'll need to use curses.
这比仅调用 input()
并接收响应要困难得多,但这是您需要的.有一个很好的资源,叫做使用Python进行程序编程,最好的起点.
Its a lot more difficult than just calling input()
and receiving a response, but it is what you'll need. There is a good resource called Curses Programming with Python which is the best place to start.
这篇关于非阻塞式键盘输入python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!