我一直在尝试在后台为芹菜安排一个任务,但我一直遇到问题。
关于目前为止我所做的事情的快速参考
我安装了celery
和django-celery
通过easy_install
,添加到INSTALLED_APPS
:
'djcelery',
'kombu.transport.django',
导入以下文件并运行syncdb:
import djcelery
djcelery.setup_loader()
BROKER_URL = "django://"
我试图在后台运行的任务是密码重置。当用户忘记密码时,我想让发送邮件任务在后台运行我所做的是我把我的forgot_password函数从views.py
移动到tasks.py
,所以它可以运行。
My tasks.py
:
from django.contrib.auth.views import password_reset
from django.shortcuts import render
from celery.decorators import task
@task()
def forgot_password(request):
if request.method == 'POST':
return password_reset(request,
from_email=request.POST.get('email'))
else:
return render(request, 'forgot_password.html')
他们现在在views.py
什么都没有。
问题是,即使我丢失了密码,我也可以发送电子邮件。我不确定它是否在后台运行。我所做的检查是:
manage.py celery worker --loglevel=info
但我得到一个错误:KeyError: 'processName'
:
C:o17mysite>manage.py celery worker --loglevel=info
-------------- celery@gg-PC v3.0.19 (Chiastic Slide)
---- **** -----
--- * *** * -- Windows-Vista-6.0.6001-SP1
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> broker: django://localhost//
- ** ---------- .> app: default:0x319c930 (djcelery.loaders.DjangoLoader
)
- ** ---------- .> concurrency: 2 (processes)
- *** --- * --- .> events: OFF (enable -E to monitor this worker)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery: exchange:celery(direct) binding:celery
[Tasks]
. accounts.tasks.forgot_password
[2013-05-15 17:49:45,279: WARNING/MainProcess] C:Python26libsite-packagesdja
ngo_celery-3.0.17-py2.6.eggdjceleryloaders.py:133: UserWarning: Using settings
.DEBUG leads to a memory leak, never use this setting in production environments
!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2013-05-15 17:49:45,292: WARNING/MainProcess] celery@gg-PC ready.
[2013-05-15 17:49:45,292: INFO/MainProcess] consumer: Connected to django://loca
lhost//.
Traceback (most recent call last):
File "C:Python26liblogging__init__.py", line 754, in emit
msg = self.format(record)
File "C:Python26liblogging__init__.py", line 637, in format
return fmt.format(record)
File "C:Python26liblogging__init__.py", line 428, in format
s = self._fmt % record.__dict__
KeyError: 'processName'
Traceback (most recent call last):
File "C:Python26liblogging__init__.py", line 754, in emit
msg = self.format(record)
File "C:Python26liblogging__init__.py", line 637, in format
return fmt.format(record)
File "C:Python26liblogging__init__.py", line 428, in format
s = self._fmt % record.__dict__
KeyError: 'processName'
有没有人可以帮助我,告诉我如果忘记密码配置可能是在后台发送电子邮件,为什么我得到这个错误KeyError: 'processName'
?
首先有一个使用芹菜发送电子邮件的简单方法:
django-celery-email
第二,你的任务是错误的。Task是后台作业,而不是视图。它应该执行最后的操作。在你的例子中,这是发送电子邮件