Send Http request at specific time

1.1k views Asked by At

I am developing an application that communicates with a server via https requests. I need one message to hit the server in a very narrow time window. I know the opening time of this time window with only a precision of half a second. I can repeat the http request as much as I want before and after, it does not matter.

For now, here's what I am doing:

  • I use Python with requests and gevent modules
  • I create a requests session, using an httpadapter with a connection pool of 1000 connections
  • I send as many orders as possible during 1 second around the time window.

    self.session = requests.Session()
    self.session.mount('https://', requests.adapters.HTTPAdapter(pool_connections=100, 
                                                                 pool_maxsize=10000))
    def sendRequest()
        self.session.post(self.url, self.body)
    
    pool = gevent.pool.Pool(size = 10000)
    while time.time() < endTime:
        g = pool.spawn(sendRequest)
        gevent.sleep(0.0000001)
    

However, I keep missing the window, which is very narrow. My code seems to only send http requests 2 milliseconds apart, even if I increase the pool size or I am trying to increase the rate at which I send my messages, to increase my chance of hitting the window. However, I am not sure how to go about this.

I wonder:

  • Is the network card the bottleneck?
  • Is python the bottleneck?
  • Am I doing something suboptimal in my code (size of the https connection pool, use of gevent, etc.)
  • Any idea how to do this well?
1

There are 1 answers

0
DevShark On BEST ANSWER

I ended up using the socket module directly, so controlling the behaviour at a lower level, and it worked. The requests module is great, but added too much latency to this particular problem.