Python中的Timer只运行x秒然后停止

时间:2016-11-21 07:36:44

标签: python multithreading

我每隔X秒获取一次天气信息,但似乎在三个线程执行完毕后,Timer停止了,程序也就停止了。问题是这是两天前工作的,我不确定是否有任何修改,因为我看不出有任何区别,但是只要程序保持运行,它就会运行。现在它运行3次并停止。

import requests
import json
import threading
import time
from threading import Thread

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()

0 个答案:

没有答案