[zeromq-dev] best way to integrate pyzmq & pyqt event loops

Marc Rossi mrossi19 at gmail.com
Wed Apr 27 23:52:44 CEST 2011


Thanks for the replies.  Like I said, I'm a newbie to Qt/Python so it may
take me a bit to get things going but I *think* I have the event loop
integration working (took what seemed to be the easiest route).  I ended up
registering a QTimer idle callback that used the zmq.Poller to check for zmq
sockets in the POLLIN state.

Marc

    ctx = zmq.Context()

    s1 = ctx.socket(zmq.SUB)
    s1.connect('tcp://hostname:5680')
    s1.setsockopt(zmq.SUBSCRIBE, '')

    poller = zmq.Poller()
    poller.register(s1, zmq.POLLIN)

    def zmq_handler():
        socks = dict(poller.poll(100))
        if (socks.has_key(s1)):
            msg = s1.recv()

    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()

    app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))

    timer = QTimer(app);
    app.connect(timer, SIGNAL("timeout()"), zmq_handler)
    timer.start(0)

    app.exec_()

On Wed, Apr 27, 2011 at 1:42 AM, MinRK <benjaminrk at gmail.com> wrote:

> I expect that using the zmqstream/ioloop code would not be a good fit
> if you are using another (Qt or otherwise) application event loop.
> Trying to use multiple Python event loops at once can get pretty
> hairy.  I think that working regular zmq Sockets into Qt, with a
> Poller triggerig Signals/Events would be a more natural fit, but I am
> not experienced with Qt.
>
> If you do use multiple event loops, you don't want them both really
> running at the same time - instead, you will probably want to run a
> single iteration of the second loop (ioloop) as a periodic callback in
> the first-class loop (presumably Qt here).
>
> For ioloop, the method for running a single iteration is by
> registering loop.stop as a callback:
>
> loop = IOLoop()
> stopper = PeriodicCallback(loop.stop, 0, loop)
> stopper.start()
>
> Now, any time you call loop.start(), it will only run one iteration
> (you can change the period of the stopper to allow the loop to run for
> a limited time, instead of a single iteration).
>
> -MinRK
>
> On Tue, Apr 26, 2011 at 21:12, Brian Granger <ellisonbg at gmail.com> wrote:
> > Hi,
> >
> > Here is example of how we are doing this in IPython:
> >
> >
> https://github.com/ipython/ipython/blob/master/IPython/zmq/ipkernel.py#L450
> >
> > But, we are not using the IOLoop in this example.  If you also want to
> > use the Qt event loop for IO related things, you will probably need to
> > integrate the file descriptors of the zeromq sockets into the Qt event
> > loop itself.  I don't really know enough about Qt to know where to
> > start with this.
> >
> > I guess the other option would to use a periodic call in IOLoop to
> > iterate the Qt event loop.  That might be easier.  Let us know what
> > you find out.
> >
> > Cheers,
> >
> > Brian
> >
> > On Tue, Apr 26, 2011 at 4:56 PM, Marc Rossi <mrossi19 at gmail.com> wrote:
> >> Looking for a little direction.  Trying to put together a PyQt app to
> view
> >> some of the google protobuf encoded data I'm slinging around from my
> C/C++
> >> apps with zmq.  Have the pyzmq/google protobuf stuff working no problem
> but
> >> trying to figure out the best way to integrate the QApplication event
> loop
> >> with the IOLoop instance I have in my script.  First thoughts are to use
> the
> >> QApplication event handler as my primary loop with idle callbacks to do
> my
> >> zmq socket polling.
> >> I'm by no means a PyQt expoert and I have to think someone has been down
> >> this path before (event though searches turned up empty), so any
> >> advice/direction that can be thrown my way would be much appreciated.
>  In
> >> the meantime I'm going to try and do zmq event processing in the qt
> >> callbacks.
> >> TIA
> >> Marc
> >> _______________________________________________
> >> zeromq-dev mailing list
> >> zeromq-dev at lists.zeromq.org
> >> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> >>
> >>
> >
> >
> >
> > --
> > Brian E. Granger
> > Cal Poly State University, San Luis Obispo
> > bgranger at calpoly.edu and ellisonbg at gmail.com
> > _______________________________________________
> > zeromq-dev mailing list
> > zeromq-dev at lists.zeromq.org
> > http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> >
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20110427/98341359/attachment.htm>


More information about the zeromq-dev mailing list