Twisted: Wait for the delay to end.

How can I “discard” the pending into the reactor so that it is processed somewhere along the road?

Situation

I have 2 programs running on localhost.

  • Twisted jsonrpc service (localhost: 30301)
  • Twisted webservice (localhost: 4000)

When someone connects to the web service, he needs to send a request to the jsonrpc service, wait for him to return with the result, and then display the result in the user's web browser (returning the value of the jsonrpc call).

I cannot figure out how to return the value of a pending jsonrpc call. When I visit the web service with my browser, I get an HTML 500 error code (did not return a byte) and Value: <Postponed to 0x3577b48>.

It returns a pending object, not the actual value of the callback.

Looking back for a couple of hours and tried many different options 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()
+4
source share
1 answer

The problem you are facing is that the web IResourceis a very old interface, even before Deferred.

A 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 a lot of processing for Deferredthe entire API.

Twisted, twisted.web.

+4

All Articles