Multiprocessing and sockets in Python

前端 未结 3 1804
后悔当初
后悔当初 2021-02-02 04:02

I am trying to make multiprocessing and socket programming work together, but, I am stuck at this point. Problem is that, I am getting this error:

  File \"multi         


        
3条回答
  •  眼角桃花
    2021-02-02 04:25

    I'm not an expert so I can't give the real explanation but if you want to use queues, you need to reduce the handle and then recreate it:

    in your main :

    client, address = serversocket.accept()
    client_handle = multiprocessing.reduction.reduce_handle(client.fileno())
    socket_queue.put(client_handle)
    

    and in your worker:

    clientHandle = queue.get()
    file_descriptor = multiprocessing.reduction.rebuild_handle(client_handle)
    clientsocket = socket.fromfd(file_descriptor, socket.AF_INET, socket.SOCK_STREAM)
    

    also

    import multiprocessing.reduction
    

    That will work with your original code. However, I am currently having problems with closing sockets in worker processes after they were created as I described.

提交回复
热议问题