[zeromq-dev] twisted/coherence and zmq

Tom Cocagne tom.cocagne at gmail.com
Wed Nov 20 16:57:38 CET 2013


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:

https://github.com/smira/txZMQ
https://github.com/cocagne/zpax/blob/master/zpax/network/zed.py

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.

Tom


On Tue, Nov 19, 2013 at 10:43 AM, Nicholas Lagaros <nicholas at lagaros.com>wrote:

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


More information about the zeromq-dev mailing list