[Twisted-Python] How to invoke the remote method based on twisted in django web app?

Boern cayson.z at gmail.com
Thu Mar 5 20:43:02 MST 2009


hi,all:
I developed a web app in diango and a remote service in twisted, and I want
to invoke the twisted remote method in django web.example:
the remote service code :

class Echoer(pb.Root):
    def remote_echo(self, task):

        print 'echoing:', task

        return task

if __name__ == '__main__':
    reactor.listenTCP(8789, pb.PBServerFactory(Echoer()))
    reactor.run()

-------------------------------------------------------------------------------------------------------------
and the djiango views.py code :

       def register_task(requst):
"""register the task"""
        .................
factory = pb.PBClientFactory()
reactor.connectTCP("localhost", 8789, factory)
d = factory.getRootObject()
d.addCallback(lambda object: object.callRemote("echo", task))
d.addCallback(lambda echo: 'server echoed: '+echo[0]+str(echo[1]))
d.addErrback(lambda reason: 'error: '+str(reason.value))
d.addCallback(util.println)
d.addCallback(lambda _: reactor.stop())
reactor.run()
        ..........................
        return HttpResponseRedirect('/push_task/') # Redirect after POST

-------------------------------------------------------------------------------------------------------------

but when I post a request to django web app ,a exception display:
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\twisted\internet\base.py", line 374,
in fi
reEvent
    DeferredList(beforeResults).addCallback(self._continueFiring)
  File "C:\Python25\Lib\site-packages\twisted\internet\defer.py", line 195,
in a
ddCallback
    callbackKeywords=kw)
  File "C:\Python25\Lib\site-packages\twisted\internet\defer.py", line 186,
in a
ddCallbacks
    self._runCallbacks()
  File "C:\Python25\Lib\site-packages\twisted\internet\defer.py", line 328,
in _
runCallbacks
    self.result = callback(self.result, *args, **kw)
--- <exception caught here> ---
  File "C:\Python25\Lib\site-packages\twisted\internet\base.py", line 387,
in _c
ontinueFiring
    callable(*args, **kwargs)
  File "C:\Python25\Lib\site-packages\twisted\internet\base.py", line 1123,
in _
reallyStartRunning
    self._handleSignals()
  File "C:\Python25\Lib\site-packages\twisted\internet\base.py", line 1068,
in _
handleSignals
    signal.signal(signal.SIGINT, self.sigInt)
exceptions.ValueError: signal only works in main thread
--------------------------------------------------------------------------------------------------------------

so ,How I correctly invoke the remote method?
-- 
Boern Parx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090306/abdd620b/attachment.html>


More information about the Twisted-Python mailing list