Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 - Fatal编程技术网

Python-用于接收消息的独立线程

Python-用于接收消息的独立线程,python,multithreading,Python,Multithreading,我在Python2.7.8中遇到了一个关于线程的问题。问题存在于编写的python聊天代码中。我将把代码包含到线程类和客户端类的不同python文件中,因为我认为问题出在这两个文件中的一个,而不是服务器代码中。当我运行Client.py文件时,我能够通过服务器与运行相同python代码的另一个客户端通信,但问题是我必须刷新.send_payloadmsg命令,以便接收另一个客户端发送的消息,或者只需在聊天中按enter键,然后作为消息发送。我想知道是否可以在不刷新聊天记录的情况下通过线程接收消息

我在Python2.7.8中遇到了一个关于线程的问题。问题存在于编写的python聊天代码中。我将把代码包含到线程类和客户端类的不同python文件中,因为我认为问题出在这两个文件中的一个,而不是服务器代码中。当我运行Client.py文件时,我能够通过服务器与运行相同python代码的另一个客户端通信,但问题是我必须刷新.send_payloadmsg命令,以便接收另一个客户端发送的消息,或者只需在聊天中按enter键,然后作为消息发送。我想知道是否可以在不刷新聊天记录的情况下通过线程接收消息

class MessageReceiver(Thread):
      def __init(self,client,connection):
          Thread.__init__(self)
          self.daemon = True
          self.client = client
          self.connection = connection
          self.stop = False

      def run(self):
          while not self.stop:
              data = self.connection.recv(8192)
              if not data:
                   break
              else:
                   self.client.receive_message(data)
          pass




class Client:
    def __init__(self, host, server_port):
            self.host = host
            self.server_port = server_port
            self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.run()

    def run(self):
            self.connection.connect((self.host, self.server_port))
            self.thread = MessageReceiver(self, self.connection)
            self.thread.start()

            while True:
                text = raw_input()
                if(text.find("login") == 0):
                    data={"request":"login","content":text[text.find("login")+6:]}
                    self.send_payload(data)
                if(text.find("names") == 0):
                    data={"request":"names","content":""}
                    self.send_payload(data)
                else:
                    data={"request":"msg","content":text}
                    self.send_payload(data)

    def disconnect(self):
        self.thread.stop = True
        self.connection.close()
        print("Disconnected")

    def receive_message(self, message):
        print("Msg: " + message)

    def send_payload(self, data):
        self.connection.send(json.dumps(data))
        print("Payload sent!")            

代码在我看来没问题。可能是服务器没有向您转发消息?如果您发布服务器代码可能会有所帮助。考虑到服务器代码有270行长,这可能有点问题。你有什么特别的想法吗?为什么我必须刷新聊天记录才能收到收到的信息?