您需要的是一个具有时间戳的缓存值的属性,以便在值过期时发出请求以获取当前值。像这样:
它可以做成装饰工,但这需要更长的时间:class Meteo():
def __init__(self):
self.last_update = 0
def Temperature(self):
if (time.time() - self.last_update) > 60: # cache the value for 60 seconds
self.API = pyowm.OWM('My API Key', config_module=None,
language='it', subscription_type=None)
self.location = self.API.weather_at_place('Rome,IT')
self.weatherdata = self.location.get_weather()
self.weather = str(self.weatherdata.get_detailed_status())
self.last_update = time.time()
self.tempvalue = self.weatherdata.get_temperature('celsius')
temperature = str(self.tempvalue.get('temp'))
return temperature
无论你多久呼叫一次温度,它最多每60秒只发出一个请求。在