Вопрос

How can I 'throw' deferred's into the reactor so it gets handled somewhere down the road?

Situation

I have 2 programs running on localhost.

  1. A twisted jsonrpc service (localhost:30301)
  2. A twisted webservice (localhost:4000)

When someone connects to the webservice, It needs to send a query to the jsonrpc service, wait for it to come back with a result, then display the result in the web browser of the user (returning the value of the jsonrpc call).

I can't seem to figure out how to return the value of the deferred jsonrpc call. When I visit the webservice with my browser I get a HTML 500 error code (did not return any byte) and Value: < Deferred at 0x3577b48 >.

It returns the deferred object and not the actual value of the callback.

Been looking around for a couple of hours and tried a lot of different variations before asking.

from txjsonrpc.web.jsonrpc import Proxy
from twisted.web import resource
from twisted.web.server import Site
from twisted.internet import reactor


class Rpc():
    def __init__(self, child):
        self._proxy = Proxy('http://127.0.0.1:30301/%s' % child)

    def execute(self, function):
        return self._proxy.callRemote(function)


class Server(resource.Resource):
    isLeaf = True

    def render_GET(self, request):
        rpc = Rpc('test').execute('test')

        def test(result):
            return '<h1>%s</h1>' % result

        rpc.addCallback(test)
        return rpc

site = Site(Server())
reactor.listenTCP(4000, site)
print 'Running'
reactor.run()
Это было полезно?

Решение

The problem you're having here is that web's IResource is a very old interface, predating even Deferred.

The quick solution to your problem is to use Klein, which provides a nice convenient high-level wrapper around twisted.web for writing web applications, among other things, adding lots of handling for Deferreds throughout the API.

The slightly more roundabout way to address it is to read the chapter of the Twisted documentation that is specifically about asynchronous responses in twisted.web.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top