user1996215
user1996215

Reputation: 1

Deferred is not called sometimes (twisted python)

I've done a quite simple web service like this:

class UploadResource(Resource):

    def onSuccess(self, result):
        self.request.write("OK")

    def onError(self, result):
        self.request.write("Error")

    def render_POST(self, request):
        request.data.save_to_disk()
        d = request.mirror("http://mirror_replica/Upload")
        d.addCallbacks(self.onSuccess, self.onError)
        return server.NOT_DONE_YET

Method request.mirror() returns a deferred which fired callbacks after http client request finished send data to another replica of the same web service

When I ran a simple stress test for /Upload method to one replica after some successful requests I got a read timeout client exception. For 1000 successful requests I'v got one read timeout exception. After some debug I figured out that sometimes onSuccess callback never called and read timeout did not caused by heavy load.

So, I've rewrote class code to this:

class UploadResource(Resource):

    def render_POST(self, request):
        request.data.save_to_disk()
        d = request.mirror("http://mirror_replica/Upload")

        def onSuccess(result):
            request.write("OK")

        def onError(self, result):
            request.write("Error")

        d.addCallbacks(onSuccess, onError)
        return server.NOT_DONE_YET

Now onSuccessCallback is always called and read timeout is gone.

The question is "Why?"

Upvotes: 0

Views: 271

Answers (1)

Glyph
Glyph

Reputation: 31910

If your Deferred is not being called, then someone is failing to call it. This could be a bug, or it could simply be that the request is taking a long time. Without seeing the code for the mirror method it's really impossible to even guess. If you could produce a complete, runnable example, that would go a long way towards diagnosing your problem.

Upvotes: 1

Related Questions