Home > Software design >  How to avoid calling a api in loops?
How to avoid calling a api in loops?

Time:07-28

I make several python programs that need to loop every 5 seconds to check via a public api if the data has evolved. Is there a solution to be "contacted" by the api when there is a change to avoid unnecessary requests that return unmodified data.

Example : If the api returns x=12 and x=12 during 10 minutes is it possible to receive a "signal" in order to send a request only 10 minutes passed to obtain the new value of x and don't send few requests with same response

Thank you

CodePudding user response:

I'm assuming that the API is a web API over HTTP.

HTTP requests work the way that it is always the client that queries the server.

Assuming that you have control over the API there are the following solutions

From a server perspective you can solve it by having the API using webhooks to inform the server when data has changed

From a client perspective you'd either look into long polling, or maintaining some sort of bidrectional communication protocol with the server where the server can notify the client of any new updates. You could look into websockets

  • Related