使用 apache 和 mod wsgi 配置 django apscheduler

问题描述

我已经通过运行以下命令在 django 项目中配置了 django apscheduler - python manage.py runapscheduler

但现在我们已将应用程序移至生产服务器并使用 Apache 和 Mod wsgi 进行配置,那么现在如何配置和运行 apscheduler。

请提出建议。

解决方法

https://medium.com/@mrgrantanderson/replacing-cron-and-running-background-tasks-in-django-using-apscheduler-and-django-apscheduler-d562646c062e 解释了如何设置包含调度程序的 AppConfig:

为此,您必须将以下内容添加到您的 settings.py

INSTALLED_APPS = [
    ...
    "django_apscheduler",]

# This scheduler config will:
# - Store jobs in the project database
# - Execute jobs in threads inside the application process
SCHEDULER_CONFIG = {
    "apscheduler.jobstores.default": {
        "class": "django_apscheduler.jobstores:DjangoJobStore"
    },'apscheduler.executors.processpool': {
        "type": "threadpool"
    },}
SCHEDULER_AUTOSTART = True

启动调度程序的文件可能如下所示:

import logging

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ProcessPoolExecutor,ThreadPoolExecutor
from django_apscheduler.jobstores import register_events,register_job

from django.conf import settings

# Create scheduler to run in a thread inside the application process
scheduler = BackgroundScheduler(settings.SCHEDULER_CONFIG)

def start():
    if settings.DEBUG:
        # Hook into the apscheduler logger
        logging.basicConfig()
        logging.getLogger('apscheduler').setLevel(logging.DEBUG)

    # Adding this job here instead of to crons.
    # This will do the following:
    # - Add a scheduled job to the job store on application initialization
    # - The job will execute a model class method at midnight each day
    # - replace_existing in combination with the unique ID prevents duplicate copies of the job
    scheduler.add_job("core.models.MyModel.my_class_method","cron",id="my_class_method",hour=0,replace_existing=True)

    # Add the scheduled jobs to the Django admin interface
    register_events(scheduler)

    scheduler.start()

以及将像这样启动调度程序的 AppConfig:

from django.conf import settings

class CoreConfig(AppConfig):
    name = "core"

     def ready(self):
        from . import scheduler
        if settings.SCHEDULER_AUTOSTART:
            scheduler.start()

(从上面链接的教程中复制的所有代码)。

但是请注意,django-apscheduler 在他们的自述文件中声明

这种简单性的代价是您需要小心确保在特定时间点只有一个调度程序处于活动状态。 (https://github.com/jcass77/django-apscheduler#quick-start)。

如果您在多线程 Django 环境中高效地运行调度程序,就我所知,这并不能保证。因此,这个解决方案绝对应该有所保留 - 更正确的方法是遵循自述文件的建议并为调度程序创建一个阻塞命令,并独立于您在专用进程中提供的服务来运行该命令。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...