django - 休伊;不在一个 Django 应用程序中运行任务

标签 django python-3.x python-huey

我有一个名为“tickets”的应用程序,它在设置文件中,可以正确导入。

INSTALLED_APPS = [
... 
"huey.contrib.djhuey",
"core",
"telefon",
"termine",
"tickets",
...
]

我正在运行 Huey 来执行后台任务,它确实运行其他两个应用程序中的所有任务,只是不在应用程序“门票”中运行。这是应用程序门票中的“helpers”模块:

from huey import crontab
from huey.contrib.djhuey import db_periodic_task, periodic_task

@periodic_task(crontab(minute="*/1"))
def checkForRunningHuey():
    logger.debug("Huey did run at {pendulum.now()}")


@db_periodic_task(crontab(minute="*/5"))
def getKissTickets():
    site_settings = Setting.load()
    if not site_settings.last_update_tickets:
        soy, now = getThisYear
        site_settings.last_update_tickets = soy
        site_settings.save()
    site_settings = Setting.load()
    ...

这是我的 Huey 配置:

HUEY = {
    "huey_class": "huey.RedisHuey",  # Huey implementation to use.
    "name": "Huey",  # Use db name for huey.
    "results": False,  # Store return values of tasks.
    "store_none": False,  # If a task returns None, do not save to results.
    "immediate": False,  # If DEBUG=True, run synchronously.
    "utc": True,  # Use UTC for all times internally.
    "blocking": True,  # Perform blocking pop rather than poll Redis.
    "connection": {
        "host": "192.168.x.xxx",
        "port": 6379,
        "db": 0,
        "connection_pool": None,  # Definitely you should use pooling!
        "read_timeout": 1,  # If not polling (blocking pop), use timeout.
        "url": None,  # Allow Redis config via a DSN.
    },
}

这是manage.py run_huey 的输出:

$ python manage.py run_huey --huey-verbose
[2020-03-18 14:17:08,249] INFO:huey.consumer:MainThread:Huey consumer started with 1 thread, PID 3100 at 2020-03-18 13:17:08.249881
[2020-03-18 14:17:08,249] INFO:huey.consumer:MainThread:Scheduler runs every 1 second(s).
[2020-03-18 14:17:08,251] INFO:huey.consumer:MainThread:Periodic tasks are enabled.
[2020-03-18 14:17:08,252] INFO:huey.consumer:MainThread:The following commands are available:
[2020-03-18 14:17:08,368] DEBUG:huey.consumer.Scheduler:Scheduler:Checking periodic tasks
[2020-03-18 14:17:08,368] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.8815112113952637
[2020-03-18 14:17:09,288] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9611930847167969
[2020-03-18 14:17:10,316] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9331824779510498
[2020-03-18 14:17:11,296] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9533252716064453
[2020-03-18 14:17:12,330] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9188826084136963
[2020-03-18 14:17:13,311] DEBUG:huey.consumer.Scheduler:Scheduler:Sleeping for 0.9387216567993164

最佳答案

当消费者运行时,它需要导入定义任务的所有模块,否则它们不会被拾取。 run_huey 管理命令将使用 autodiscover_modules("tasks") 自动发现任何模块名称 tasks.py

为了获取定期任务,您需要在 tasks.py 中定义它们,或者让 tasks.py 导入定义它们的模块.

关于django - 休伊;不在一个 Django 应用程序中运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60740653/

相关文章:

python - 如何在python中计算用户输入的文本字符串中有多少个单词

python - 既然 Celery 4 已经放弃了 Windows 支持,那么 Windows 上的(Python 3)任务队列的最佳选择是什么?

python - 使用 Elastic Beanstalk 上的 Supervisor 在后台运行 Huey 任务队列

django - 使用 Django REST Framework 进行身份验证返回 405

python - 在 PyMySQL 中使用 Django 2.2 时遇到问题

python - 如何在 Django 中编写自己的装饰器?

python - 如何使用 Huey 创建带有参数的任务函数?

python - 当我使用对象名称时,我得到 'DeferredAttribute' 对象没有属性 'get'

python - 如何使用 df.iterrows() 匹配同一列中的前一行值?

mysql - 如何将每个sql结果存储在循环生成的数据帧中?