I have created 2 different ways of returning from one of my route-decorated functions:
Method 1:
return HTTPResponse(status=200, body=myBody)
Method 2 (This is done in order to for after_request hook to read the same response object, for now.):
response = bottle.response.copy()
response.status = 200
response.body = myBody
return response
myBody is a string, NOT a dict (our upstream library already converts the dict into a string via json.dumps()), and myBody is also large: it is a dictionary with 1 key, but a 600+ member list as the value.
My problem is: Method 1 returns very fast to the calling client (client is using requests library via a POST request).
Method 2 is about 10 times slower (the calling client waits about 2 mins to get the response, versus 2 seconds in method 1).
I've also disabled any after_hook
logic just to isolate any other effects.
Any hint as to what might be the root cause?
The most efficient (fastest) way to return your large string is to simply return an iterable. E.g.,
return [myBody]
In particular: (1) don't copy the response object, and (2) 200 is the default response code, so no need to specify it.
Q: Why do I return a list (containing a single string) instead of just the string?
A: We could return myBody
, but return [myBody]
is preferred. Here's why:
Applications must return an iterable yielding byte strings. You may return a string (because strings are iterable) but this causes most servers to transmit your content char by char.