Python中的Timer只运行x秒然后停止 [英] Timer in Python runs only for x seconds and then stops
本文介绍了Python中的Timer只运行x秒然后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我每隔X秒获取天气信息,但似乎在三个线程执行完毕后,Timer才停止。问题是这是两天前工作的,我不确定是否有任何修改,因为我看不出有什么显着差异。
I am getting the information for weather every X seconds but it seems that after the three threads have executed the Timer just stops. The problem is that this was working two days ago and I am not sure if anything has been modified as I can't see a significant difference.
import requests
import json
import threading
import time
from threading import Thread
import _sqlite3
def main():
threading.Timer(10, giveVelingrad).start()
threading.Timer(15, giveSofia).start()
threading.Timer(25, givePlovdiv).start()
def giveVelingrad():
city = "Velingrad"
response = requests.get("http://api.openweathermap.org/data/2.5/weather?q=" +city+ "&appid=2e8707d1eb97fbbba33ef766d9ed80ac&units=metric")
weather = response.json()
www = time.strftime("%d.%m.%y %H.%M.%S")
print("\n",www)
print("The weather for ", weather['name'])
print("The temperature is ", weather['main']['temp'])
def giveSofia():
city = "Sofia"
response = requests.get("http://api.openweathermap.org/data/2.5/weather?q=" +city+ "&appid=2e8707d1eb97fbbba33ef766d9ed80ac&units=metric")
weather = response.json()
www = time.strftime("%d.%m.%y %H.%M.%S")
print("\n",www)
print("The weather for ", weather['name'])
print("The temperature is ", weather['main']['temp'])
def givePlovdiv():
city = "Plovdiv"
response = requests.get("http://api.openweathermap.org/data/2.5/weather?q=" +city+ "&appid=2e8707d1eb97fbbba33ef766d9ed80ac&units=metric")
weather = response.json()
www = time.strftime("%d.%m.%y %H.%M.%S")
print("\n",www)
print("The weather for ", weather['name'])
print("The temperature is ", weather['main']['temp'])
if __name__ == '__main__':
main()
我尝试了什么:
请帮忙,因为我似乎无法理解什么是问题
What I have tried:
Please help as I can't seem to understand what's the problem
推荐答案
你的给定函数只会运行一次,参见 17.1。线程化 - 基于线程的并行性 - Python 3.5.2文档 [ ^ ]。
Your give functions will only run once, see 17.1. threading — Thread-based parallelism — Python 3.5.2 documentation[^].
这篇关于Python中的Timer只运行x秒然后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文