【发布时间】:2020-09-19 10:29:29
【问题描述】:
我有这样的代码:
def1():
a = requests.get(url)
...
def2():
something that calls def1()
def3():
something that calls def2()
def4():
something that calls def2()
defN():
something that calls def(2)
我想安排所有这些函数每隔固定时间 X 执行一次。为此,我使用了调度模块,第一个函数没有问题。
schedule.every.hour.do(def1)
schedule.every.hour.do(def2)
...
但我希望 defN 从 url 收集数据(可能是因为它调用 def2 调用 def1),但与其他立即使用 url 信息的函数不同,我希望 defN 持有url 请求一个变量中相同的固定时间段 X 的数据,然后继续执行它的代码。
该功能的目的是每小时收集上一小时的url信息,然后对上一小时数据和当前小时数据进行运算。当然,在程序运行的第一个小时内,defN 不会返回任何内容。有没有办法将函数的第一部分暂停一小时,然后每小时安排一次?
我尝试在 defN 中使用 time.sleep() 但它也停止了其他功能两个小时。我学习了 Thread 模块,但我从来没有让它正常工作。我把这段代码放在除 defN 之外的所有其他函数中:
t = threading.Thread(target=defN)
t.start()
但它没有用。我该如何解决这个问题?
【问题讨论】:
标签: python-3.x function datetime time python-multithreading