<div dir="ltr"><div><div>The event loop conflicts you mention can be avoided if you use a wrapper library to integrate zmq sockets into the Twisted event loop. There are two options for doing so that I can think of off-hand:<br>
<br><a href="https://github.com/smira/txZMQ">https://github.com/smira/txZMQ</a><br><a href="https://github.com/cocagne/zpax/blob/master/zpax/network/zed.py">https://github.com/cocagne/zpax/blob/master/zpax/network/zed.py</a><br>
<br></div>txZMQ is a bit more well rounded and makes zmq sockets look and behave a bit more like other Twisted sockets. Zed, on the other hand, is a bare-bones event loop integrator that avoids "twistifying" the zmq API. Either would probably work for you but I'd lean towards txZMQ since it's more widely used.<br>
<br></div>Tom<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Nov 19, 2013 at 10:43 AM, Nicholas Lagaros <span dir="ltr"><<a href="mailto:nicholas@lagaros.com" target="_blank">nicholas@lagaros.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><u></u>
<div style="font-family:Verdana,Geneva,sans-serif">
<p>I am trying to create simple publisher that will send media player updates to another process.  I understand there are event loop conflicts between zmq and twisted.  Can anyone recommend a better way to code this?</p>

<p> </p>
<p>#! /usr/bin/python2.5<br><br>import sys<br>sys.path.insert(1, './lib')<br><br>import cPickle as pickle<br><br>from twisted.internet import reactor<br>from coherence.base import Coherence<br>from coherence.upnp.devices.control_point import ControlPoint<br>
from coherence.upnp.core import DIDLLite<br><br>import zmq<br><br>ZonePlayers = []<br>Son = [0,0,0,0,0,0,0,0,0,0,0,0]<br>SonArray = [{}]<br>sck = None<br><br>class SonosPlayer:<br>        SonosCount = 0<br><br>        def __init__(self, uuid):<br>
                global SonArray<br>                self.uuid = uuid<br><br>                self.index = SonosPlayer.SonosCount<br>                SonosPlayer.SonosCount += 1<br>                SonArray[self.index] = {'uuid':self.uuid}<br>
<br>        def returnSonosCount(self):<br>                return SonosPlayer.SonosCount<br><br>        def state_variable_change(self, variable):<br>                global sck<br>                global SonArray<br>                print "variable: %s" % <a href="http://variable.name" target="_blank">variable.name</a><br>
                #print "variable value: %s" % variable.value<br>                print "update from: %s" % self.uuid<br>                #print SonArray<br><br>                if <a href="http://variable.name" target="_blank">variable.name</a> == 'CurrentTrackMetaData':<br>
                        if variable.value != None and len(variable.value)>0:<br>                                try:<br>                                        elt = DIDLLite.DIDLElement.fromString(variable.value)<br>                                        for item in elt.getItems():<br>
                                                SonArray[self.index]['creator'] = item.creator<br>                                                SonArray[self.index]['albumArtist'] = item.albumArtist<br>                                                SonArray[self.index]['artist'] = item.artist<br>
                                                SonArray[self.index]['albumArtURI'] = item.albumArtURI<br>                                                SonArray[self.index]['title'] = item.title<br>                                                SonArray[self.index]['album'] = item.album<br>
                                except SyntaxError:<br>                                        print "current error"<br>                                        return<br>                elif <a href="http://variable.name" target="_blank">variable.name</a> == 'NextTrackMetaData':<br>
                        if variable.value != None and len(variable.value)>0:<br>                                try:<br>                                        elt = DIDLLite.DIDLElement.fromString(variable.value)<br>                                        for item in elt.getItems():<br>
                                                SonArray[self.index]['next_creator'] = item.creator<br>                                                SonArray[self.index]['next_albumArtist'] = item.albumArtist<br>
                                                SonArray[self.index]['next_artist'] = item.artist<br>                                                SonArray[self.index]['next_albumArtURI'] = item.albumArtURI<br>
                                                SonArray[self.index]['next_title'] = item.title<br>                                                SonArray[self.index]['next_album'] = item.album<br>                                except SyntaxError:<br>
                                        print "next error"<br>                                        return<br>                elif <a href="http://variable.name" target="_blank">variable.name</a> == 'TransportState':<br>
                        SonArray[self.index]['TransportState'] = variable.value<br><br>                f = open("SonosEvents.pkl", "wb")<br>                pickle.dump(SonArray, f, protocol=2)<br>
                f.close()<br><br>                msg = self.uuid + " " + SonArray[self.index]['title']<br>                print msg<br>                sck.send(msg)<br><br></p>
<p>def media_renderer_found(client,udn):<br>        global ZonePlayers<br>        global Son<br>        Xudn = udn[5:-3]<br>        print ("%s:%s:%s" % (udn, Xudn, ZonePlayers))<br>        if Xudn in ZonePlayers:<br>
                print "media_renderer_found", udn<br>                Son[SonosPlayer.SonosCount] = SonosPlayer(Xudn)<br>                client.av_transport.subscribe_for_variable('CurrentTrackMetaData', Son[SonosPlayer.SonosCount].state_variable_change)<br>
                client.av_transport.subscribe_for_variable('NextTrackMetaData', Son[SonosPlayer.SonosCount].state_variable_change)<br>                client.av_transport.subscribe_for_variable('LastChange', Son[SonosPlayer.SonosCount].state_variable_change)<br>
                client.av_transport.subscribe_for_variable('TransportState', Son[SonosPlayer.SonosCount].state_variable_change)<br><br>def media_renderer_removed(udn):<br>    print "media_renderer_removed", udn<br>
<br>def start():<br>        control_point = ControlPoint(Coherence({'logmode':'warn'}), auto_client=['MediaRenderer'])<br>        control_point.connect(media_renderer_found, 'Coherence.UPnP.ControlPoint.MediaRenderer.detected')<br>
        control_point.connect(media_renderer_removed, 'Coherence.UPnP.ControlPoint.MediaRenderer.removed')<br><br><br>f = open("ZonePlayers.pkl", "rb")<br>ZonePlayers = pickle.load(f)<br>f.close()<br>
<br>context = zmq.Context()<br>sck = context.socket(zmq.PUB)<br>sck.bind('tcp://<a href="http://127.0.0.1:9999" target="_blank">127.0.0.1:9999</a>')<br><br>reactor.callWhenRunning(start)<br>reactor.run()<br><br></p>

<div> </div>
</div>
<br>_______________________________________________<br>
zeromq-dev mailing list<br>
<a href="mailto:zeromq-dev@lists.zeromq.org">zeromq-dev@lists.zeromq.org</a><br>
<a href="http://lists.zeromq.org/mailman/listinfo/zeromq-dev" target="_blank">http://lists.zeromq.org/mailman/listinfo/zeromq-dev</a><br>
<br></blockquote></div><br></div>