Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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套接字recv_Python - Fatal编程技术网

线程中的Python套接字recv

线程中的Python套接字recv,python,Python,这是我的问题。我在lua上有一个服务器,它通过套接字发送数据。数据不断流动——它是一个交换事务流。我的python脚本作为客户端应该接收数据 def listen(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = socket.gethostname() sock.connect(("localhost", 1111)) with BytesIO() as response_b

这是我的问题。我在lua上有一个服务器,它通过套接字发送数据。数据不断流动——它是一个交换事务流。我的python脚本作为客户端应该接收数据

def listen():

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = socket.gethostname()
    sock.connect(("localhost", 1111))

    with BytesIO() as response_bytes:
            while True:    
                try:
                    fragment = sock.recv(8196)    
                    print("Фрагмент: {}".format(fragment))
                except Exception:
                    pass

if __name__ == "__main__":

    t2 = threading.Thread(listen())
    t2.start()

    while True:

        print ("test") 
主线程等待sock.recv8196行。我希望来自套接字的数据被并行接受,并且主流继续工作。当前代码会阻塞main的性能,直到执行listen。我不熟悉Python中的多个任务。可以做哪些决定?

必须将侦听函数传递给threading.Thread。您在主线程中调用函数,等待它完成,然后传递其返回值,返回值为None,因为函数从不返回任何内容

t2 = threading.Thread(target = listen)