我导入Schedule
到Flask
。我的项目包含WSGI
但我很少知道之间的关系Flask
和WSGI
。现在我有三个主要文件:
wsgi.py
:由其他工具自动生成。app.py
:我在这里提出客户要求。test.py
:用于测试Schedule
。我想启动服务器启动时需要花很长时间的任务。这是的一部分wsgi.py
:
# -*- coding: utf-8 -*-
from threading import Thread
import test
t = Thread(target=test.job)
t.start()
if __name__ == '__main__':
...
如您所见,我启动了一个线程,然后在其中工作。这是我的test.py
。
import schedule
def job():
schedule.every(1).seconds.do(pr)
def pr():
print("I'm working...")
我的问题是job
永远不会开始。
我发现了我的问题。我从不让计划执行作业。现在wsgi.py
看起来像这样。
# -*- coding: utf-8 -*-
from threading import Thread
import test
schedule.every(1).seconds.do(test.job)
t = Thread(target=test.run_schedule)
t.start()
if __name__ == '__main__':
...
和test.py
:
import schedule
import time
start_time = time.time()
def job():
print("I'm working..." + str(time.time() - start_time))
def run_schedule():
while True:
schedule.run_pending()
time.sleep(1)
为了在单独的线程中工作,我创建了一个线程,并在此线程中每1ms循环一次。在循环中,schedule
调用run_pending
以调用job
if超时(在我的情况下为1s)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句