芹菜没有发现项目内部的任务

发布于 2025-02-07 12:53:53 字数 1853 浏览 1 评论 0原文

我有一个项目myproject和一个应用程序app

在myproject内部,我有tasks.py

from celery import shared_task

@shared_task
def add(x, y):
    return x + y

在我的应用程序中,我有以下tasks.py

from celery import shared_task
from django.core.mail import send_mail


@shared_task
def send_email_task(email):
    "background task to send an email asynchronously"
    subject = 'Hello from Celery'
    message = 'This is a test email sent asynchronously with Celery.'
    time.sleep(1)
    return send_mail(
        subject,
        message,
        '[email protected]',
        [email],
        fail_silently=False
    )

在运行芹菜工人时,我仅从应用程序中看到共享任务,而不是从myproject中

(myprojectenv) root@ubuntu-s-1vcpu-1gb-blr1-02:/etc/myproject# celery -A myproject worker -l info
/etc/myprojectenv/lib/python3.8/site-packages/celery/platforms.py:840: SecurityWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

  warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . app.tasks.send_email_task

[2022-06-15 09:16:44,314: INFO/MainProcess] Connected to amqp://hpoddar:**@IPADDRESS:5672/vhostcheck
[2022-06-15 09:16:44,322: INFO/MainProcess] mingle: searching for neighbors

看到此处我的芹菜.py文件

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.autodiscover_tasks()

I have a project myproject and an app app.

Inside myproject I have tasks.py

from celery import shared_task

@shared_task
def add(x, y):
    return x + y

Inside my app I have the following tasks.py

from celery import shared_task
from django.core.mail import send_mail


@shared_task
def send_email_task(email):
    "background task to send an email asynchronously"
    subject = 'Hello from Celery'
    message = 'This is a test email sent asynchronously with Celery.'
    time.sleep(1)
    return send_mail(
        subject,
        message,
        '[email protected]',
        [email],
        fail_silently=False
    )

When running the celery workers I see only shared tasks from the app and not from myproject

(myprojectenv) root@ubuntu-s-1vcpu-1gb-blr1-02:/etc/myproject# celery -A myproject worker -l info
/etc/myprojectenv/lib/python3.8/site-packages/celery/platforms.py:840: SecurityWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

  warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . app.tasks.send_email_task

[2022-06-15 09:16:44,314: INFO/MainProcess] Connected to amqp://hpoddar:**@IPADDRESS:5672/vhostcheck
[2022-06-15 09:16:44,322: INFO/MainProcess] mingle: searching for neighbors

Here is my celery.py file

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.autodiscover_tasks()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2025-02-14 12:53:53

您需要添加导入:

从django.conf导入设置

You need add the import:

from django.conf import settings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文