AMQClient initialization arguments

Asked by reverri


I'm only starting to learn Twisted and was wondering if it would be possible to remove the initialization arguments from AMQClient(). It seems from the Twisted documentation that arguments should be passed to the Factory and then accessed in the Protocol via self.factory.

I'm trying to implement an AmqpFactory and currently have to provide the arguments to an AmqpProtocol:

class AmqpProtocol(AMQClient):
 def __init__(self):
  delegate = TwistedDelegate()
  vhost = '/'
  spec = txamqp.spec.load('../apparatus/specs/standard/amqp0-8.xml')
  AMQClient.__init__(self,delegate, vhost, spec)

 def connectionMade(self):

class AmqpFactory(protocol.ClientFactory):
 protocol = AmqpProtocol

 def __init__(self,user=None,password=None):
  self.user = user or 'guest'
  self.password = password or 'guest'

Maybe I am heading in the wrong direction. Any hints would be greatly appreciated.

Thank you,

Question information

English Edit question
txAMQP Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
Revision history for this message
reverri (reverri) said :

If you bring the __init__() operations into connectionMade() for both AMQClient and FrameReceiver you can persist data in the factory rather than the protocol.

Here is a quick example:

This makes it easier to use reactor.connectTCP() and later integrate with twistd

Revision history for this message
reverri (reverri) said :

In case anyone is interested, the changes above are not necessary. You can just overwrite the buildProtocol() method of the factory to supply the init arguments for the protocol.

class AmqpFactory(protocol.ClientFactory):
 protocol = AmqpProtocol

 def __init__(self,spec=None,vhost=None,user=None,password=None):
   self.delegate = TwistedDelegate()
   self.spec = txamqp.spec.load('../apparatus/specs/standard/amqp0-8.xml')
   self.user = user or 'guest'
   self.password = password or 'guest'
   self.vhost = '/'

 def buildProtocol(self):
  p = AMQClient(self.delegate,self.vhost,self.spec)
  p.factory = self
  return p

Revision history for this message
Esteve Fernandez (esteve) said :

Sorry for not replying to this earlier, I've been very busy lately and couldn't work on txAMQP.

Thanks for posting your solution, it works great. BTW, if you don't need to share data across all protocol instances, you can also use ClientCreator.