squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

Response is not send before calling add_done_callback #53

Open kesavkolla opened 7 years ago

kesavkolla commented 7 years ago

I had to do some heavy work after sending request response. I added add_done_callback to request and wrote work intensive task. The response didn't sent to client till the add_done_callback is done. Here is the code I've:

def makereq(request):
    def cb(r):
        for x in range(10):
            requests.get(url)
            time.sleep(0.5)
    request.add_done_callback(cb)
    return request.Response(text='done')

I was expecting the done response will be immediately sent to client. The response was held up since the callback is not done.

squeaky-pl commented 7 years ago

Yes, that's by design. It runs after the view finished, and response is rendered but as it accepts the request which is bound to response, it needs to block it, otherwise the objects could be gone already.

This is not the API you are looking for. As a workaround use threads instead. I have in my mind some future offloading APIs but this one is bound to request - response lifecycle. It could have been named better.