recv_pyobj behaves different from Thread to Process
<p><br>
I am trying to read data from sockets on 6 different proceses at a time for perfomance sake. I made a test with opening 6 threads and do a socket read from each one and another test with opening 6 sub-processes and read a different socket. Thread reading works fine and it looks like this: </p>

<pre><code>class ZMQServer:
    context = zmq.Context()
    socket = None
    ZMQthread = None

    def __init__(self, port, max_size):
        self.socket = self.context.socket(zmq.SUB)
        self.socket.setsockopt(zmq.SUBSCRIBE, '')
        self.socket.connect("tcp://127.0.0.1:" + str(port))

    def StartAsync(self):
        ZMQthread = threading.Thread(target=self.Start)
        ZMQthread.start()

   def Start(self):
        print "ZMQServer:Wait for next request from client on port: %d" % self.port
        while True:
            print "Running another loop"
            try: 
                message = self.socket.recv_pyobj()
            except: 
                print "ZMQServer:Error receiving messages"

if __name__ == '__main__':
    zmqServers = [None] * 6
    for idx in range (0, 6):
        zmqServers[idx] = ZMQServer(DTypes.PORTS_RECREGISTER[idx], 1024)
        zmqServers[idx].StartAsync()
</code></pre>

<p>This will display:</p>

<blockquote>
  <p>ZMQServer:Wait for next request from client on port: 4994<br>
      Running another loop<br>
      ZMQServer:Wait for next request from client on port: 4995<br>
      Running another loop<br>
      ZMQServer:Wait for next request from client on port: 4996<br>
      Running another loop<br>
      ZMQServer:Wait for next request from client on port: 4997<br>
      Running another loop<br>
      ZMQServer:Wait for next request from client on port: 4998<br>
      Running another loop<br>
      ZMQServer:Wait for next request from client on port: 4999<br></p>
</blockquote>

<p>IMPORTANT: I receive data on socket wen I send it.<br>
Now, I need to acheieve the same behaviour but only instead of threads use Processes so the octa core will use more of the processor. The code looks like this:</p>

<pre><code>context = zmq.Context()
def CreateSocket(port):
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, '')
    socket.connect("tcp://127.0.0.1:" + str(port))

def Listen(socket, port):
    print "ZMQServer:Wait for next request from client on port: %d" % port
    while True:
        print "Running another loop"
        try: 
            message = socket.recv_pyobj()
            print "ZMQServer:Received request: %s" % message
        except: 
            print "ZMQServer:Error receiving messages"
            continue

  #I'm trying first only with 1 Process - 1 socket:

   if __name__ == '__main__':
        port = DTypes.PORTS_RECREGISTER[0]
        socket = CreateSocket(port)
        proc = Process(target=Listen, args=(socket, port))
        proc.start()
        proc.join()
</code></pre>

<p>The output is strange:</p>

<blockquote>
  <p>ZMQServer:Wait for next request from client on port: 4994<br>
      Running another loop<br>
      ZMQServer:Error receiving messages<br>
      Running another loop<br>
      ZMQServer:Error receiving messages<br>
      Running another loop<br>
      ZMQServer:Error receiving messages<br>
      ............<br></p>
</blockquote>

<p>AND VERY IMPORTANT is that I do not receive data on the socket when I send it<br>
So, from what I can understand:<br>
1. Method enters and on every while loop the socket is changed?<br>
2. OR The recv_pyobj is not blocking anymore?<br>
Does anybody experienced that before? Does anybody know how to correclty do the multi-process socket reading?
<br>Thank you</p>

<p>Actually, the problem was because of the passing SOCKET type argument to a Process/Thread. It seems that pickling, behind the scenes that serializes the data passed to callback method does not serialize well the SOCKET argument. <br>
So, I changed it so I would not pass a socket argument:</p>

<pre><code>context = zmq.Context()

def ListeToSocket(port):
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, '')
    socket.connect("tcp://127.0.0.1:" + str(port))
    print "Wait for data on port: %d" % port
    while True:
        try:
            message = socket.recv_pyobj()
            print "PORT:%d Received data: %s" % (port, message)
        except:
            print "PORT:%d: Error receiving messages" % port

if __name__ == '__main__':
    for idx in range(0, DTypes.SOCKET_MAX):
        proc1 = Process(target=ListeToSocket, args=(DTypes.PORTS_RECREGISTER[idx], ))
        proc1.start()
</code></pre>

python multithreading sockets subprocess