python - Can pyzmq publishers be operated from class instances? -


i have following code publisher, instantiates few class instances , publishes messages.

however, don't receive @ subscriber side.

publisher

import zmq import time multiprocessing import process  class senddata:     def __init__(self, msg, port):         self.msg = msg         self.port = port         ctx = zmq.context()         self.sock = ctx.socket(zmq.pub)         self.sock.bind('tcp://127.0.0.1:'+str(self.port))         time.sleep(1)      def sender(self):         self.sock.send_json(self.msg)  def main():     device, port in zip(['2.2.2.2', '5.5.5.5'],[5001, 5002]):         msg = {device:'some random message'}         instance = senddata(device, port)         process(target=instance.sender).start()  if __name__ == "__main__":     main() 

subscriber

import zmq  ctx = zmq.context() recv_sock1 = ctx.socket(zmq.sub) recv_sock1.connect('tcp://127.0.0.1:5001') recv_sock1.setsockopt(zmq.subscribe, '')  recv_sock2 = ctx.socket(zmq.sub) recv_sock2.connect('tcp://127.0.0.1:5002') recv_sock2.setsockopt(zmq.subscribe, '')  while true:     if recv_sock1.poll(10):         msg = recv_sock1.recv_json()         print msg      if recv_sock2.poll(10):         msg = recv_sock2.recv_json()         print msg 

i had subscribers started before publishers publish anything. also, can see tcp connections in established connections made.

  • pyzmq version 16.0.0
  • python version 2.7

q1: 0mq publishers supported class instances?
q2: missing?

a1: yes, are.

a2: conflicts of scope-of-use v/s zero-sharing, 1 of zeromq maxims

once original publisher code being executed in main(), class instantiation process creates ( i.e. inside main()-process scope-of-use ), via .__init__() constructor-method, it's own context() -instance, belongs ( incl. of it's derived child-objects ( sockets et al ) ) main()-process.

next, call process(...) initiates few processes, receive class-instances ( pitty these have created zeromq non-share-able toys ) main()-scope-of-use.

solution?

a possible dirty quick hack defer zeromq context() instantiation -- yes, move .__init__() .adeferredsetup() executed under different scope-of-use inside each of spinned-of process()-process, different main()-process , ought done, zero-sharing safely obeyed.

class senddata:     def __init__(self, msg, port):         self.msg      = msg         self.port     = port          self.notsetup = true         self.ctx      = none         self.sock     = none         # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ l8r         # ctx         = zmq.context()         # self.sock   = ctx.socket( zmq.pub )         # self.sock.bind( 'tcp://127.0.0.1:' + str( self.port ) )         # time.sleep( 1 )         # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ l8r      def sender( self ):         if      self.notsetup:                 self.adeferredsetup()          self.sock.send_json( self.msg )      def adeferredsetup( self ):     # create i/o-threads in process(), not main()         self.ctx    = zmq.context()          self.sock   = self.ctx.socket( zmq.pub )         self.sock.bind( 'tcp://127.0.0.1:' + str( self.port ) )         time.sleep( 1 ) 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -