0

I have written a TCP proxy server in python 2.7 which creates a greenlet for each connection using gevent. When the socket closes, I close out the greenlet. I am using grequest.map to POST the data a given connection receives. I am using grequests because I want the greenlet to yield while it's waiting for the HTTP response to the POST.

When I test the server running locally with only a single connection, sometimes it takes up to 0.5 seconds from when I map the grequest to when urllib3 logs that it is starting the connection. It is not consistent, mostly it makes the request within 0.05s, but occasionally it is much longer.

I am logging the time it takes from when I tell grequests to make the request as well as the requests reported elapsed time. In addition, I am timestamping all my logs, so I can see in the logs that sometimes there is a large time gap.

This is how the code looks on the server:

req = grequests.post(endpoint, data=json.dumps(body), headers=headers, timeout=1)
a = time.time()
socket_log_debug(address, context, "Starting post...")
grequests.map([req])
b = time.time()
socket_log_info(address, context, "Time1: {}".format(b-a))
socket_log_info(address, context, "Time2: {}".format(req.response.elapsed.total_seconds()))

And this is an example of the resulting log when it takes a long time to post:

tcp-app: 17-04-04 12:24:38.432 root DEBUG: connection=49416, serialnum=TST-000, message='Starting post...'
tcp-app: 17-04-04 12:24:38.843 requests.packages.urllib3.connectionpool DEBUG: Starting new HTTPS connection (1): xxxxxxxxxxxxxxx
tcp-app: 17-04-04 12:24:39.509 requests.packages.urllib3.connectionpool DEBUG: xxxxxxxxxxxxxxx "POST /xx HTTP/1.1" 200 0
tcp-app: 17-04-04 12:24:39.510 root INFO: connection=49416, serialnum=TST-000, message='Time1: 1.0784201622'
tcp-app: 17-04-04 12:24:39.510 root INFO: connection=49416, serialnum=TST-000, message='Time2: 0.669282'

Why does the request sometimes take so long to go out? It seems like this happens when the endpoint is also slow to response (0.6s is slow given the average) but I am not sure if or why these are correlated.

oregano
  • 816
  • 9
  • 25
  • I am not sure but is not monkey patching designed for this? I mean why do you need another library? I am sorry if I am not getting something right. But if I am right then you should have good info here - http://stackoverflow.com/questions/9501663/how-enable-requests-async-mode – SRC Apr 04 '17 at 16:50
  • grequests uses monkey patching; it's actually recommended in the thread you linked to. As far as I can tell, it's just a well-tested and well-used async wrapper for requests. I ran this same test without grequests (monkey patching it myself) and got the same result. – oregano Apr 04 '17 at 17:20
  • Is it usually the [first request](https://github.com/kennethreitz/grequests/issues/24) that's slower? – brennan Apr 05 '17 at 14:47

0 Answers0