【问题标题】:non-blocking keyboard input非阻塞键盘输入
【发布时间】:2015-12-07 04:04:40
【问题描述】:

当对等点不断侦听传入连接(新对等点)并通过终端(用户输入)向其他对等点发送命令时,我正在构建一个 p2p 系统。我一直在寻找新的同伴时很难从键盘上寻找用户输入。

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 检查来自新连接的对等方的传入消息。我只需要一种发送消息的方式。

【问题讨论】:

    标签: python linux


    【解决方案1】:

    简短的回答是您需要使用curses

    这比仅仅调用input() 并收到回复要困难得多,但这正是您所需要的。有一个很好的资源叫做Curses Programming with Python,这是最好的起点。

    【讨论】:

      猜你喜欢
      • 2010-10-01
      • 2011-01-16
      • 1970-01-01
      • 2016-07-04
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多