A
A
AusTiN2013-04-28 23:45:35
Python
AusTiN, 2013-04-28 23:45:35

Asynchronous requests to a Twisted application?

Hey Habr.
There is a twisted application that accesses some remote service through an external python library:

import externalLib
def render_POST(self, request):
    #some code here
    result = self.doQuery(...)
    return json.dumps(result)

def doQuery(self, ...):
    #some code
    return externalLib.request()

The problem is that if something slows down in the doQuery function (or even on externalLib.request) - for example, the remote service takes a long time to respond - then all other http requests to the twisted application will also wait for the result of processing the previous request. (If anything, externalLib makes an http request via requests.)
Connoisseurs, please tell me - how to make the application asynchronous and not make new requests wait for previous ones to complete?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tricesimus, 2013-04-28
@AusTiN

from twisted.internet import reactor, threads

def doLongCalculation():
    # .... do long calculation here ...
    return 3

def printResult(x):
    print x

# run method in thread and get result as defer.Deferred
d = threads.deferToThread(doLongCalculation)
d.addCallback(printResult)
reactor.run()

But, according to recent observations, the callback will not be called earlier than in a second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question