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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/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
Multithreading 如何将数据接收到线程中?_Multithreading_Sockets_Communication - Fatal编程技术网

Multithreading 如何将数据接收到线程中?

Multithreading 如何将数据接收到线程中?,multithreading,sockets,communication,Multithreading,Sockets,Communication,这是我从客户机VMware(或笔记本电脑)接收数据的代码。 它处理接收到的图像数据,并将结果值发送回客户端。它将只与少数客户正常工作,因为它一次只处理一个客户。所以我想修改我的代码以接收数据并用独立的线程处理它,每个客户机一个专用线程 但问题是线程和主进程似乎共享一些相同的东西。我为我的线程和主进程分配了不同的端口号,但是当主进程与客户端连接时,线程不能接受来自客户端的连接,当线程与客户端连接时,主进程也不能接受连接。 它们不仅不能接受连接,而且共享同一队列,这使得recv(16)和recv(1

这是我从客户机VMware(或笔记本电脑)接收数据的代码。 它处理接收到的图像数据,并将结果值发送回客户端。它将只与少数客户正常工作,因为它一次只处理一个客户。所以我想修改我的代码以接收数据并用独立的线程处理它,每个客户机一个专用线程

但问题是线程和主进程似乎共享一些相同的东西。我为我的线程和主进程分配了不同的端口号,但是当主进程与客户端连接时,线程不能接受来自客户端的连接,当线程与客户端连接时,主进程也不能接受连接。 它们不仅不能接受连接,而且共享同一队列,这使得recv(16)和recv(1024)函数出错

我最终想要的是,我的主进程除了通信之外什么也不做,或者做别的事情,只有线程和客户端通信一起工作

import socket
serverSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSock.bind(("", someport_no))
serverSock.listen(3)

count = 0
while 1:
    print("Waiting for connection ...")
    client, addr = serverSock.accept()
    print("Connection established")

    recvData = client.recv(16) #Receives file size
    fileSize = int().from_bytes(recvData, "little") #Decoding file size
    print("The file size : ", fileSize)
    totalRecv = bytes() #Convert totalRecv variable's data type to bytes object
    recvCount = bytes()
    while True:
        recvData = client.recv(1024)# Puts the data received from the client 1024kb each.
        totalRecv += recvData # Stacks received data into totalRecv
        if len(totalRecv) == fileSize:#Ends receiving when the size matches.
            break
    with open(dir + "rune_client.png", "wb") as f:
        f.write(totalRecv)
    # client, addr = serverSock.accept()
    print("Image analizing...")
    first = time.perf_counter()
    result = detects() # processes the image
    client.send(result.encode("utf-8"))
    second = time.perf_counter()
    print('Detect time : ',second - first)
    count += 1
    print('Server access count : ', count)