Daniel Saggo
Daniel Saggo

Reputation: 108

Is there a way to reload an API every X seconds/minutes

So I'm working on a tkinter gui for my raspberry pi 3 and i've some information coming through an API call, the thing is, i need to restart the system before it will update the api. I would like to have the API update automatically every X seconds or X minutes. I hope you understand what i'm trying to tell you. Here is the code for the API call

    r2 = requests.get(' http://api.adviceslip.com/advice')
    advice_result= r2.json()

Upvotes: 0

Views: 1905

Answers (2)

Maunil Shah
Maunil Shah

Reputation: 65

def reloadapi():
    threading.Timer(5.0, reloadapi).start()
    r2 = requests.get(' http://api.adviceslip.com/advice')
    advice_result= r2.json()
    return advice_result

try this

Upvotes: 0

Maunil Shah
Maunil Shah

Reputation: 65

import threading
import requests

def reloadapi():
    threading.Timer(5.0, reloadapi).start()
    r2 = requests.get(' http://api.adviceslip.com/advice')
    advice_result= r2.json()

reloadapi()

Here, your api is reloaded in every 5 seconds

Upvotes: 1

Related Questions