【问题标题】:Python socket threading with kbhit带有 kbhit 的 Python 套接字线程
【发布时间】:2013-03-20 14:33:25
【问题描述】:

我不知道 python 中的 msvcrt.kbhit() 如何在我单击需要按下的指定键后立即打印出数据。看起来while循环需要再次循环才能打印出我想要的输出。有人可以帮帮我吗。这是我的代码:

def run(self):
    global state
    print "\nClient connection received!\n"
    self.channel.send("Status: Server connection received")
    while 1:
        ctr = 1
        while 1:
            self.clientmess = self.channel.recv(Buffer)
            if msvcrt.kbhit():
                if msvcrt.getch() == 's':
                    print "stop"
                    break
    #the codes belo is what i will want for self.clientmess will be so its not necessary I think to put

【问题讨论】:

  • 你到底有什么问题?
  • 当代码从客户端接收数据并且我按下键盘上的's'键时,当我按下键时打印停止不打印,它需要在打印“停止”之前接收另一个数据打印在屏幕上

标签: python multithreading sockets keypress


【解决方案1】:

大多数情况下,您的程序会阻塞在 recv 调用中,因此在收到一些数据之前,它不会执行 kbhit+getch。如果您需要立即处理键盘输入,您可能需要使套接字成为非阻塞并在循环中轮询套接字和键盘,并在出现时处理来自它们的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多