I am having some very strange problems with reqCurrentime().
Look at the below script, which simply gets the current time and the remote time, compares them, waits for a little bit, and then tries again.
async def main2():
N = 300
times = np.zeros(N)
ib = ibs.IB()
await ib.connectAsync("gateway", 4004, clientId=20, timeout=60)
for i in range(300):
local_tm = datetime.datetime.now(datetime.UTC)
try:
server_tm = await asyncio.wait_for(ib.reqCurrentTimeAsync(), 10.0)
except TimeoutError:
print(f"{i} :Timeout!")
times[i] = 1.0
else:
times[i] = (server_tm - local_tm).total_seconds()
t2 = datetime.datetime.now(datetime.UTC)
processing = (t2 - local_tm).total_seconds()
await asyncio.sleep(0.5)
print(f"{i} : diff = {times[i]:.2f} sec, processing = {processing:.2f} sec")
print(f"Avg = {np.mean(times)}, std = {np.std(times)}")
asyncio.run(main2())
The strange thing here is that if I await asyncio.sleep(1)
or longer, the program works more or less as expected. Below is an example of such normal output:
0 : diff = -0.88 sec, processing = 0.00 sec
1 : diff = -0.88 sec, processing = 0.00 sec
2 : diff = -0.88 sec, processing = 0.00 sec
3 : diff = -0.88 sec, processing = 0.00 sec
4 : diff = -0.89 sec, processing = 0.00 sec
5 : diff = -0.89 sec, processing = 0.00 sec
6 : diff = -0.89 sec, processing = 0.00 sec
However, once I await asyncio.sleep(0.5)
or anything below 1, I see something as follows:
0 : diff = -0.94 sec, processing = 0.00 sec
1 :Timeout!
1 : diff = 1.00 sec, processing = 10.01 sec
2 : diff = -0.95 sec, processing = 0.00 sec
3 :Timeout!
3 : diff = 1.00 sec, processing = 10.01 sec
4 : diff = -0.97 sec, processing = 0.00 sec
5 :Timeout!
5 : diff = 1.00 sec, processing = 10.01 sec
6 : diff = -0.98 sec, processing = 0.00 sec
7 :Timeout!
7 : diff = 1.00 sec, processing = 10.01 sec
8 : diff = -1.00 sec, processing = 0.00 sec
In other words, reqCurrentTimeAsync()
times out every other time, even though I have set the timeout to be 10 seconds! If I wait for 0.5 second between each call of reqCurrentTime(), I am only sending 2 messages per second.
(I was debugging some strange freezing up in production and finally produced this minimal example to reproduce the problem.)
Does anything know what might be going on?