Python async got interrupted by 5 seconds

46 Views Asked by At

I was making requests to binance exchange through https and the receive time window by default is 5 seconds. Most of my requests were normal but a few of them trigger the error saying the timestamp of request is out of receive window.

Firstly I thought this is caused by an internet problem and I tcpdumped a pcapfile and sslkey, reading it on wireshark, and I found surprisingly the first tcp text was sent five seconds later than it should(just beyond the receive window limit).

The 5 seconds delay happended every times the system triggered the error above. Is it possible the "async with" part to be interrupted by other process and force back in 5 seconds? And how could I solve this problem?

This is the code I use to send https request.

async with aiohttp.ClientSession() as client:
            session = await self.requests_method(client, method)
            self.logger.info(f"requests send, order ts: {body['timestamp'] / 1000}, send ts: {time.time()}, inst: {body['symbol']}")
            async with session(url, headers=header) as rsp:
                self.logger.info(f"response received, order ts: {body['timestamp'] / 1000}, receive ts: {time.time()}, inst: {body['symbol']}")

For example, the output sending timestamp and receiving timestamp are below and the pcap I captured show the TCP stream of this whole connection. Send Info Receive Info TCP stream

0

There are 0 best solutions below