【问题标题】:django_cron executes only once, scheduler not working?django_cron 只执行一次,调度器不工作?
【发布时间】:2017-03-22 23:40:00
【问题描述】:

我必须按时执行任务,因为我正在使用 django_cron。 在设置中:

INSTALLED_APPS = [
    'mailsnake',
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Avaana_web',
    'rest_framework',
    'rest_framework.authtoken',
    'django_cron',

]

和 cron.py

    from django_cron import CronJobBase, Schedule,cronScheduler
    import datetime,os
    class MyCronJob(CronJobBase):
        RUN_EVERY_MINS = .3
        RETRY_AFTER_FAILURE_MINS = 5
        ALLOW_PARALLEL_RUNS = True
        schedule = Schedule(run_every_mins=RUN_EVERY_MINS,      
  retry_after_failure_mins=RETRY_AFTER_FAILURE_MINS)
        code = 'my_app.my_cron_job'
        def do(self):
            print("hello")

但是当我运行时

$ python manage.py runcrons
 hello

只有一次输出显示并结束。

如何在每 30 秒后获得输出。

【问题讨论】:

  • 我也安装了 django_cron。通过 $ pip install django_cron
  • 尝试使用python -u manage.py runcrons运行
  • 如何使用django_cron 每 30 分钟发送一次电子邮件。正在查看
  • 我的 cron.py 包含:from django_cron import CronJobBase, Schedule, get_class from django.core.mail import EmailMessage class my_scheduled_job(CronJobBase): RUN_EVERY_MINS = 30 schedule = Schedule(run_every_mins=RUN_EVERY_MINS) print("schedule") print(schedule) code = 'Sana_web.my_cron_job' def do(self): email = EmailMessage('title', 'body', to=[email]) email.send()

标签: django django-models django-views django-rest-framework django-redis


【解决方案1】:

几件事:

一个。看起来您的 cron 作业配置不正确。根据文档,您需要在设置路径中创建一个 CRON_CLASSES 列表,指向您的 cron 类的完全限定包名称,如下所示:

CRON_CLASSES = [
    "my_app.cron.MyCronJob",
    # ...
]

b.此外,通过 python manage 运行它并不意味着它会继续运行多次。您可能仍需要从 cron 作业运行“python manage.py runcrons”。当您调用 manage.py runcrons 时,您的日程安排只会决定它是否需要运行。在此处查看更多详细信息:http://django-cron.readthedocs.io/en/latest/installation.html

【讨论】:

    猜你喜欢
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多