我需要在Django应用程序中实现一个预定任务。DBader的schedule似乎是这个工作的一个很好的候选者,但是当它作为Django项目的一部分运行时,它似乎没有产生预期的效果。
具体来说,作为一个独立的程序,这项工作很好:
import schedule
import time
import logging
log = logging.getLogger(__name__)
def handleAnnotationsWithoutRequests(settings):
'''
From settings passed in, grab job-ids list
For each job-id in that list, perform annotation group/set logic [for details, refer to handleAnnotationsWithRequests(requests, username)
sans requests, those are obtained from db based on job-id ]
'''
print('Received settings: {}'.format(str(settings)))
def job():
print("I'm working...")
#schedule.every(3).seconds.do(job)
#schedule.every(2).seconds.do(handleAnnotationsWithoutRequests, settings={'a': 'b'})
invoc_time = "10:33"
schedule.every().day.at(invoc_time).do(handleAnnotationsWithoutRequests, settings={'a': 'b'})
while True:
schedule.run_pending()
time.sleep(1)
但在Django上下文中运行的这段(等效)代码不会导致调用。
^{pr2}$因此来自handleAnnotationsWithoutRequests()
的日志不会出现在控制台上。
this scheduling library与Django兼容吗?有什么使用示例可以让我参考吗?
我怀疑这里有一些线程问题。或许还有更好的替代品可以使用?欢迎提出建议。
提前谢谢你。
对于web服务器,您可能不希望运行进程内的程序:
https://github.com/Tivix/django-cron已证明是一个有效的解决方案。在
还有重量级冠军芹菜和芹菜。在
相关问题 更多 >
编程相关推荐