Django 中的作业调度 [英] Job Scheduling in Django

查看:62
本文介绍了Django 中的作业调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我们的 Django 应用中实现一个计划任务.DBader 的 schedule 似乎很适合这项工作,但是当它作为 Django 项目的一部分运行时,它似乎没有产生预期的效果.

I need to implement a scheduled task in our Django app. DBader's schedule seems to be a good candidate for the job, however when run it as part of a Django project, it doesn't seem to produce the desired effect.

具体来说,这作为一个独立程序运行良好:

Specifically, this works fine as an independent program:

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 上下文中运行的这个(等效的)代码不会导致调用.

But this (equivalent) code run in Django context doesn't result in an invocation.

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)

所以来自 handleAnnotationsWithoutRequests() 的日志不会出现在控制台上.

so the log from handleAnnotationsWithoutRequests() doesn't appear on the console.

这个调度库是否与 Django 兼容?有没有可以参考的使用示例?

Is this scheduling library compatible with Django? Are there any usage samples that one could refer me to?

我怀疑这里有一些线程问题.也许有更好的替代品可以使用?欢迎提出建议.

I'm suspecting some thread issues are at work here. Perhaps there are better alternatives to be used? Suggestions are welcome.

提前致谢.

推荐答案

对于 Web 服务器,您可能不想要在进程中运行的东西:

For web servers, you probably don't want something that runs in-process:

用于定期作业的进程内调度程序 [...]

https://github.com/Tivix/django-cron 已证明有效解决方案.

https://github.com/Tivix/django-cron has proven a working solution.

还有重量级冠军 Celery 和 Celerybeat.

There's also the heavyweight champion Celery and Celerybeat.

这篇关于Django 中的作业调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆