Job Scheduling in Django
我需要在我们的django应用程序中实现一个预定的任务。DBader的时间表似乎是该工作的一个很好的候选者,但是当作为Django项目的一部分运行它时,它似乎不会产生期望的效果。
具体来说,作为一个独立的程序,这很好地工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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上下文中运行的这个(等效的)代码不会导致调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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 ] ''' log.info('Received settings: {}'.format(str(settings))) def doSchedule(settings): ''' with scheduler library Based on time specified in settings, invoke .handleAnnotationsWithoutRequests(settings) ''' #settings will need to be reconstituted from the DB first #settings = {} invocationTime = settings['running_at'] import re invocationTime = re.sub(r'([AaPp][Mm])',"", invocationTime) log.info("Invocation time to be used: {}".format(invocationTime)) schedule.every().day.at(invocationTime).do(handleAnnotationsWithoutRequests, settings=settings) while True: schedule.run_pending() time.sleep(1) |
号
因此,来自
这个调度库与Django兼容吗?有没有可以参考的使用示例?
我怀疑这里有一些线程问题。也许还有更好的选择可以使用?欢迎提出建议。
提前谢谢。
对于Web服务器,您可能不需要在进程中运行的内容:
An in-process scheduler for periodic jobs [...]
号
https://github.com/tivix/django-cron已经证明了一个有效的解决方案。
还有重量级冠军芹菜和芹菜节拍。