tags:

views:

51

answers:

1

Hi,

I'm using iPython right now to interactively set up a Twisted network. The script that I run in iPython describes best of what I have to do:

import router, pdb

# creates nodes which encapsulate RandomVector and VectorAdder objects
a = router.LocalNode(router.RandomVector, '/topic/a_c')
b = router.LocalNode(router.RandomVector, '/topic/b_c')
c = router.LocalNode(router.VectorAdder)
c.registerAsListener('/topic/a_c')
c.registerAsListener('/topic/b_c')

def conn():
    global a
    global b
    a.onConnect()
    b.onConnect()


router.r.loadOnConnect(conn)
router.STOMPconnect(router.r)
router.reactor.run()

What's happening is that conn() is being passed to a Twisted protocol, which runs conn() every time the connection is lost.

onConnect looks like this:

def onConnect(self):
    self._lc = LoopingCall(self.advance)
    self._lc.start(1)

Whenever onConnect gets run, I get the error:

TypeError: 'unbound method onConnect() must be called with RandomVector instance as first argument (got nothing instead)'

Does anyone know why this is happening?

+2  A: 

I don't think this is a scoping issue. Are you sure you don't need to use:

a = router.LocalNode(router.RandomVector(), '/topic/a_c')
b = router.LocalNode(router.RandomVector(), '/topic/b_c')

i.e. instantiate the RandomVector you pass to LocalNode?

This recommendation is triggered by the Unboud method error message : an unbound method is one which is linked to a class and not to an instance / object. As your message complains about a.onConnect being an unbound method, it looks like you need to pass an instance and not a class.

gurney alex
This sounds like the right answer to me. It might help to explain a bit more about what an unbound method in Python is, though.
Jean-Paul Calderone