Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在等待输入时保持终端更新?_Python_Multithreading_Input - Fatal编程技术网

Python 如何在等待输入时保持终端更新?

Python 如何在等待输入时保持终端更新?,python,multithreading,input,Python,Multithreading,Input,我正在制作一个聊天服务器,希望聊天在您键入时实时更新,而不是每次按enter键时更新,因为输入会使我的代码挂起 目前我有: def send(): message = input() #Send message def receive(): while unread_messages > 0: #Recive messages while True: send() receive() 是否可以在后台持续运行message rec

我正在制作一个聊天服务器,希望聊天在您键入时实时更新,而不是每次按enter键时更新,因为输入会使我的代码挂起

目前我有:

def send():
    message = input()
    #Send message

def receive():
    while unread_messages > 0:
        #Recive messages

while True:
    send()
    receive()

是否可以在后台持续运行message receival进程而不必每次等待用户输入?

您可以使用线程或异步。在下面的示例中,我使用线程运行一个函数,该函数在程序等待用户输入时在终端中打印一条消息(“嘿”)

from threading import Thread

class Receiver(Thread):
  def __init__ (self, msg):
    Thread.__init__(self)
    self.message = msg

  def run(self):
    while(True):
      print(self.message)
      delay(1)

a = Receiver("heey")
a.start()
a = input("write your message")