Skip to content
Advertisement

Calling an External API That Fails Randomly

I am using Django server to call a Korean government’s weather API to retrieve weather data for around 1800 locations.

However, this weather API results in time out most of the time. I tried giving resting periods. For example, for every request, it will sleep for 0.8 seconds and after every 30 requests, it will sleep for 30 seconds. But this does not really work well for 1800 requests. At one point, it was successful. All other times, it ended up with fail.

In this kind of situation, what else should I do to make sure that 1800 requests are completed within one hour? Is there any way for this to restart the request sequence from the exact point it failed previously?

For someone who is curious about what this code looks like, I am posting it below:

JavaScript

Advertisement

Answer

Try using a while loop on a single request,

JavaScript

This way, whenever a request failed, it will just retry that request. But you have to make sure that the request will eventually be successful or else it will loop itself endlessly. You may add a trial counter to count the number of trials and break if the trials exceed, for say like 5 times, to prevent infinite loop.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement