我有一个项目
myproject
和一个应用程序 app
。
在我的项目中我有
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
)
运行 celery 工作人员时,我只看到应用程序中的共享任务,而不是我的项目中的
(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
这是我的
celery.py
文件
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.autodiscover_tasks()
您需要添加导入:
from django.conf import settings