How to resolve timeout errors when making API calls on Azure Virtual Machine in Python

SJ 0 Reputation points
2024-05-20T18:05:12.5066667+00:00

I am encountering timeout errors when making API calls to api.kite.trade and api.telegram.org in Python scripts running on an Azure Virtual Machine. Although It does not occur every time, but at least 5-10 times a day. How can I address this issue on Azure Virtual Machine?

Errors

  • HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=10).
  • HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=10)

The above two APIs are being called in the script. Please let me know if you could suggest something to mitigate this issue. Following are some additional details if needed

Cloud Details

  • Operating system: Linux (Ubuntu)
  • Size: Standard F4s v2 (4 vcpus, 8 GiB memory
  • Location: Central India (Zone 1)

PS: I am using REST endpoints for making requests in Python. Also, I am using the docker container to keep the same environment for local or cloud

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,700 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 49,261 Reputation points
    2024-05-20T18:59:56.82+00:00

    Timeouts in APIs happen. Your code has to be written to handle that and try again if it is supported. I know very little about Python but a quick google for the docs shows that when creating an HTTP connection object you can provide a timeout. Set this to a value that is sufficiently long enough for a request to succeed while also not causing a stall in your app. I believe this timeout is for connecting to the remote server. The get (or other request functions I assume) also accepts a timeout and indicates the timeout for read/write requests. If your timing out receiving data then increase the timeout to a more reasonable value. It is generally recommended that you make this customizable rather than hard coding it into your app.

    0 comments No comments