Django:获取Django-cron运行 [英] Django: Getting Django-cron Running

查看:420
本文介绍了Django:获取Django-cron运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Django-cron 运行,但似乎只有在点击网站一次后运行。我正在使用Virtualenv。

I am trying to get Django-cron running, but it seems to only run after hitting the site once. I am using Virtualenv.

任何想法为什么它只运行一次?

Any ideas why it only runs once?

在我的路径上,我添加了django_cron的位置:'/ Users / emilepetrone / Workspace / zapgeo / zapgeo / django_cron'

On my PATH, I added the location of django_cron: '/Users/emilepetrone/Workspace/zapgeo/zapgeo/django_cron'

我的Django应用程序中的cron.py文件:

My cron.py file within my Django app:

from django_cron import cronScheduler, Job

from products.views import index

class GetProducts(Job):

    run_every = 60

    def job(self):
        index()

cronScheduler.register(GetProducts)

class GetLocation(Job):

    run_every = 60

    def job(self):
        index()

cronScheduler.register(GetLocation)


推荐答案

strong>第一个可能的原因

The first possible reason

django_cron / base.py 中有一个变量:

# how often to check if jobs are ready to be run (in seconds)
# in reality if you have a multithreaded server, it may get checked
# more often that this number suggests, so keep an eye on it...
# default value: 300 seconds == 5 min
polling_frequency = getattr(settings, "CRON_POLLING_FREQUENCY", 300)

所以,检查的最小间隔启动任务的时间是 polling_frequency 。您可以通过在项目的 settings.py 中设置来更改它:

So, the minimal interval of checking for time to start your task is polling_frequency. You can change it by setting in settings.py of your project:

CRON_POLLING_FREQUENCY = 100 # use your custom value in seconds here

启动Django Web后至少要启动一个工作一次服务器。

To start a job hit your server at least one time after starting Django web server.

第二个可能的原因

The second possible reason

你的工作有一个错误,它没有排队(如果你的工作引发了一个例外,那么他们的排队等级标志设置为'f')。在这种情况下,它存储在表'django_cron_job'字符串值'f'的字段'queued'中。您可以测试它进行查询:

Your job has an error and it is not queued (queued flag is set to 'f' if your job raises an exception). In this case it stores in field 'queued' of table 'django_cron_job' string value 'f'. You can test it making the query:

从django_cron_job选择排队;

如果您更改作业的代码,则该字段可能保留为f。所以,如果你纠正你的工作的错误,你应该手动设置在排队的字段:'t'。或者表 django_cron_cron 中的执行标记为t。这意味着你的应用程序。当您的任务正在进行中时,服务器已停止。在这种情况下,您应该手动将其设置为'f'。

If you change the code of your job the field may stay as 'f'. So, if you correct the error of your job you should manually set in queued field: 't'. Or the flag executing in the table django_cron_cron is 't'. It means that your app. server was stopped when your task was in progress. In this case you should manually set it into 'f'.

这篇关于Django:获取Django-cron运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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