I am creating a child process using python multiprocessing. Parent will be creating a socket as zmq.PUB. Child will be subscribing to that socket. But the problem is child is not receiving any message.
This code is working in windows. But, when I try with RHEL 7.5, it is not working.
import multiprocessing
import zmq
import os
import time
port = '5555'
def child():
print("Child pid: ", os.getpid())
context = zmq.Context.instance()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, str.encode(''))
socket.connect("tcp://localhost:%s" % port)
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)
socks = dict(poller.poll())
while (True):
for key, value in socks.items():
print(key, " ", value)
time.sleep(5)
print('receiving data')
if socket in socks and socks[socket] == zmq.POLLIN:
try:
string = socket.recv_string(flags=zmq.NOBLOCK)
except (zmq.error.Again, Exception) as e:
break
print('data received: ', string)
def main():
print("Parent pid: ", os.getpid())
context = zmq.Context.instance()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)
childproc = multiprocessing.Process(name='childproc',target=child)
childproc.daemon = False
childproc.start()
while (True):
print('data publishing')
socket.send_string("hello")
time.sleep(5)
In windows the result is:
Parent pid: 5536
data publishing
Child pid: 13964
data publishing
<zmq.sugar.socket.Socket object at 0x000001E5BB6B3BA8> 1
data publishing
receiving data
data received: hello
<zmq.sugar.socket.Socket object at 0x000001E5BB6B3BA8> 1
data publishing
receiving data
data received: hello
......
......
In Linux the output is:
Parent pid: 21850
data publishing
Child pid: 21854
data publishing
data publishing
data publishing
data publishing
data publishing
data publishing
data publishing
..........
..........
Is it the problem with using PUB/SUB?
How can I use ZMQ for this parent to child communication?
If ZMQ does not support this, which other library can be used instead?
If I use queue and pipes from multiprocessing module, What possible problems are there, If the messages are JSON objects?