Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7674023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:38:50+00:00 2026-05-31T16:38:50+00:00

Hi I am having some issues wrapping some ZMQ pull clients in Python classes.

  • 0

Hi I am having some issues wrapping some ZMQ pull clients in Python classes. These classes are instanced and called in a Subprocess via the multiprocessing module. When the clients are functions all works but when they are classes the poller.poll() hangs.

The code bellow has both versions: One works, the other doesn’t. Why?

import zmq
import time
import sys
import random
from  multiprocessing import Process

def server_push(port="5556"):
    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.bind("tcp://*:%s" % port)
    print "Running server on port: ", port
    # serves only 5 request and dies
    for reqnum in range(10):
        if reqnum < 6:
            socket.send("Continue")
        else:
            socket.send("Exit")
            break
        time.sleep (1) 

def server_pub(port="5558"):
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.bind("tcp://*:%s" % port)
    publisher_id = random.randrange(0,9999)
    print "Running server on port: ", port
    # serves only 5 request and dies
    for reqnum in range(10):
        # Wait for next request from client
        topic = random.randrange(8,10)
        messagedata = "server#%s" % publisher_id
        print "%s %s" % (topic, messagedata)
        socket.send("%d %s" % (topic, messagedata))
        time.sleep(1)    


class Client:
    def __init__(self,port_push, port_sub):
        context = zmq.Context()
        self.socket_pull = context.socket(zmq.PULL)
        self.socket_pull.connect ("tcp://localhost:%s" % port_push)
        print "Connected to server with port %s" % port_push
        self.socket_sub = context.socket(zmq.SUB)
        self.socket_sub.connect ("tcp://localhost:%s" % port_sub)
        self.socket_sub.setsockopt(zmq.SUBSCRIBE, "9")
        print "Connected to publisher with port %s" % port_sub
        # Initialize poll set


    def __call__(self):
        poller = zmq.Poller()
        poller.register(self.socket_pull, zmq.POLLIN)
        poller.register(self.socket_sub, zmq.POLLIN)
        # Work on requests from both server and publisher
        should_continue = True
        print "listening"
        while should_continue:
            print "hello"
            socks = dict(poller.poll())
            print poller
            if self.socket_pull in socks and socks[self.socket_pull] == zmq.POLLIN:
                message = self.socket_pull.recv()
                print "Recieved control command: %s" % message
                if message == "Exit": 
                    print "Recieved exit command, client will stop recieving messages"
                    should_continue = False

                if self.socket_sub in socks and socks[self.socket_sub] == zmq.POLLIN:
                    string = self.socket_sub.recv()
                    topic, messagedata = string.split()
                    print "Processing ... ", topic, messagedata

def client(port_push, port_sub):
    context = zmq.Context()
    socket_pull = context.socket(zmq.PULL)
    socket_pull.connect ("tcp://localhost:%s" % port_push)
    print "Connected to server with port %s" % port_push
    socket_sub = context.socket(zmq.SUB)
    socket_sub.connect ("tcp://localhost:%s" % port_sub)
    socket_sub.setsockopt(zmq.SUBSCRIBE, "9")
    print "Connected to publisher with port %s" % port_sub
    # Initialize poll set
    poller = zmq.Poller()
    poller.register(socket_pull, zmq.POLLIN)
    poller.register(socket_sub, zmq.POLLIN)
    # Work on requests from both server and publisher
    should_continue = True
    while should_continue:
        socks = dict(poller.poll())
        if socket_pull in socks and socks[socket_pull] == zmq.POLLIN:
            message = socket_pull.recv()
            print "Recieved control command: %s" % message
            if message == "Exit": 
                print "Recieved exit command, client will stop recieving messages"
                should_continue = False

        if socket_sub in socks and socks[socket_sub] == zmq.POLLIN:
            string = socket_sub.recv()
            topic, messagedata = string.split()
            print "Processing ... ", topic, messagedata

if __name__ == "__main__":
    # Now we can run a few servers 
    server_push_port = "5556"
    server_pub_port = "5558"
    Process(target=server_push, args=(server_push_port,)).start()
    Process(target=server_pub, args=(server_pub_port,)).start()
    #~ Process(target=client,args=(server_push_port,server_pub_port)).start()
    Process(target=Client(server_push_port,server_pub_port)).start()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:38:51+00:00Added an answer on May 31, 2026 at 4:38 pm

    Edit1: this is not quite correct…give me a few moments to get it right…

    I think you may be invoking the Client class the wrong way. I’m not an expert with this, but I think your client should be subclassed from Process, and then run using the .start() function. So, define your Client class like this:

    class Client(Process):
        def __init__(self, port_push, port_sub):
            (...) # your class init code here...make sure indentation is correct
    

    Then at the end where you run the servers, create an instance of your Client class and start it like so:

    client_class = Client(port_push, port_sub)
    client_class.start()
    

    Edit2: Here’s an edited version of fccoelho’s code that works for me.

    The biggest problem appears to be that the ZMQ initialization stuff needs to be done in the __call__ method, not in __init__. I suspect this is due to how memory is allocated in multiprocessing, in that the __init__ function will be done in the parent process, while the __call__ function is done in the child process with a separate memory space. Apparently ZMQ doesn’t like this. I’ve also added some wait times to prevent the client from connecting to the server before the server is ready, and to prevent the server from sending messages before the client subscribes. Also using 127.0.0.1 instead of localhost (my computer doesn’t like localhost for some reason). Also removed the annoying print messages around the poll call in the client, and fixed the indentation problem where the client checks the poll results on the pubsub socket.

    import zmq
    import time
    import sys
    import random
    from  multiprocessing import Process
    
    def server_push(port="5556"):
        context = zmq.Context()
        socket = context.socket(zmq.PUSH)
        socket.bind("tcp://127.0.0.1:%s" % port)
        print "Running server on port: ", port
        time.sleep(1.0)
        # serves only 5 request and dies
        for reqnum in range(10):
            if reqnum < 6:
                socket.send("Continue")
            else:
                socket.send("Exit")
                print 'Push server sent "Exit" signal'
                break
            time.sleep(0.4) 
    
    def server_pub(port="5558"):
        context = zmq.Context()
        socket = context.socket(zmq.PUB)
        socket.bind("tcp://127.0.0.1:%s" % port)
        socket.setsockopt(zmq.HWM, 1000)
        publisher_id = random.randrange(0,9999)
        print "Running server on port: ", port
        time.sleep(1.0)
        # serves only 5 request and dies
        for reqnum in range(10):
            # Wait for next request from client
            topic = random.randrange(8,10)
            messagedata = "server#%s" % publisher_id
            print "%s %s" % (topic, messagedata)
            socket.send("%d %s" % (topic, messagedata))
            time.sleep(0.4)    
    
    
    class Client:
        def __init__(self,port_push, port_sub):
            self.port_push = port_push
            self.port_sub = port_sub
            # Initialize poll set
    
        def __call__(self):
            time.sleep(0.5)
            print 'hello from class client!'
            context = zmq.Context()
            self.socket_pull = context.socket(zmq.PULL)
            self.socket_pull.connect ("tcp://127.0.0.1:%s" % self.port_push)
            print "Connected to server with port %s" % self.port_push
            self.socket_sub = context.socket(zmq.SUB)
            self.socket_sub.connect ("tcp://127.0.0.1:%s" % self.port_sub)
            self.socket_sub.setsockopt(zmq.SUBSCRIBE, "9")
            print "Connected to publisher with port %s" % self.port_sub
    
            poller = zmq.Poller()
            poller.register(self.socket_pull, zmq.POLLIN)
            poller.register(self.socket_sub, zmq.POLLIN)
            # Work on requests from both server and publisher
            should_continue = True
            print "listening"
            while should_continue:
                # print "hello"
                socks = dict(poller.poll())
                # print poller
                if self.socket_pull in socks and socks[self.socket_pull] == zmq.POLLIN:
                    message = self.socket_pull.recv()
                    print "Recieved control command: %s" % message
                    if message == "Exit": 
                        print "Recieved exit command, client will stop recieving messages"
                        should_continue = False
    
                if self.socket_sub in socks and socks[self.socket_sub] == zmq.POLLIN:
                    string = self.socket_sub.recv()
                    topic, messagedata = string.split()
                    print "Processing ... ", topic, messagedata
    
    def client(port_push, port_sub):
        print 'hello from function client!'
        context = zmq.Context()
        socket_pull = context.socket(zmq.PULL)
        socket_pull.connect ("tcp://127.0.0.1:%s" % port_push)
        print "Connected to server with port %s" % port_push
        socket_sub = context.socket(zmq.SUB)
        socket_sub.connect ("tcp://127.0.0.1:%s" % port_sub)
        socket_sub.setsockopt(zmq.SUBSCRIBE, "9")
        print "Connected to publisher with port %s" % port_sub
        # Initialize poll set
        poller = zmq.Poller()
        poller.register(socket_pull, zmq.POLLIN)
        poller.register(socket_sub, zmq.POLLIN)
        # Work on requests from both server and publisher
        should_continue = True
        while should_continue:
            socks = dict(poller.poll(1000))
            if socket_pull in socks and socks[socket_pull] == zmq.POLLIN:
                message = socket_pull.recv()
                print "Recieved control command: %s" % message
                if message == "Exit": 
                    print "Recieved exit command, client will stop recieving messages"
                    should_continue = False
    
            if socket_sub in socks and socks[socket_sub] == zmq.POLLIN:
                string = socket_sub.recv()
                topic, messagedata = string.split()
                print "Processing ... ", topic, messagedata
    
    if __name__ == "__main__":
        # Now we can run a few servers 
        server_push_port = "5556"
        server_pub_port = "5558"
        Process(target=server_push, args=(server_push_port,)).start()
        Process(target=server_pub, args=(server_pub_port,)).start()
        # Process(target=client,args=(server_push_port,server_pub_port)).start()
        Process(target=Client(server_push_port,server_pub_port)).start()
    

    Finally, here’s a cleaner implementation of multi-process pubsub that’s very bare-bones, but demonstrates things more clearly:

    import zmq
    from multiprocessing import Process
    import time
    
    class ServerPubSub(Process):
        def __init__(self, port, n):
            Process.__init__(self)
            self.port = port
            self.n = n
    
        def run(self):
            self.context = zmq.Context()
            self.pub = self.context.socket(zmq.PUB)
            self.pub.bind('tcp://127.0.0.1:%d' % self.port)
            self.pub.setsockopt(zmq.HWM, 1000)
    
            time.sleep(1)
    
            end = False
            for i in range(self.n):
                print 'SRV: sending message %d' % i
                self.pub.send('Message %d' % i)
                print 'SRV: message %d sent' % i
                time.sleep(0.2)
    
            self.pub.close()
    
    class ClientPubSub(Process):
        def __init__(self, port, n):
            Process.__init__(self)
            self.port = port
            self.n = n
    
        def run(self):
            self.context = zmq.Context()
            self.sub = self.context.socket(zmq.SUB)
            self.sub.connect('tcp://127.0.0.1:%d' % self.port)
            self.sub.setsockopt(zmq.SUBSCRIBE, '')
            self.poller = zmq.Poller()
            self.poller.register(self.sub, zmq.POLLIN)
    
            end = False
            count = 0
            while count < self.n:
                ready = dict(self.poller.poll(0))
                if self.sub in ready and ready[self.sub] == zmq.POLLIN:
                    msg = self.sub.recv()
                    print 'CLI: received message "%s"' % msg
                    count += 1
    
            self.sub.close()
    
    if __name__ == "__main__":
        port = 5000
        n = 10
        server = ServerPubSub(port, n)
        client = ClientPubSub(port, n)
    
        server.start()
        client.start()
    
        server.join()
        client.join()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having some issues with the ... in ObjectiveC. I'm basically wrapping a method and
Having some issues with code not executing within the classes I created and thought
I'm having some issues filling a list in Python. I keep getting a list
I'm having some issues wrapping my head around the concept of classloading, I've been
I am having some issues with these background images I am using for a
Having some issues getting my head around the differences between UTF-8, UTF-16, ASCII and
I'm having some issues with an asp.net implementation of this JQuery facebook style autocomplete
I'm having some issues with some jQuery code, and I'm hoping and thinking that
I'm having some issues with my code. This is probably due to some design
I am having some issues with logging. After reviewing JBoss Seam source code, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.