Celery使用困难:函数对象没有'delay'属性
最近我一直在忙于软件开发,已经成功地让celery按照我的要求工作。
我用它成功地发送了电子邮件,现在我尝试用几乎完全相同的代码(在重启所有进程后)通过Twilio发送短信。
但是我总是遇到以下问题:
File "/Users/Rob/Dropbox/Python/secTrial/views.py", line 115, in send_sms
send_sms.delay(recipients, form.text.data)
AttributeError: 'function' object has no attribute 'delay'
我的代码如下:
@celery.task
def send_email(subject, sender, recipients, text_body):
msg = Message(subject, sender=sender)
for email in recipients:
msg.add_recipient(email)
msg.body = text_body
mail.send(msg)
@celery.task
def send_sms(recipients, text_body):
for number in recipients:
print number
num = '+61' + str(number)
print num
msg = text_body + 'this message to' + num
client.messages.create(to=num, from_="+14804054823", body=msg)
从我的views.py中调用send_email.delay时工作得很好,但每次调用send_sms.delay时都会出现上面的错误。
如果有人能帮我解决这个问题,我会非常感激。
-- 按要求提供:
@app.route('/send_mail', methods=['GET', 'POST'])
@roles_accepted('Admin')
def send_mail():
form = SendMailForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
emails = db.session.query(User.email).all()
list_emails = list(zip(*emails)[0])
send_email.delay('Subject', 'sender@example.com', list_emails, form.text.data)
return render_template('send_generic.html', form=form)
@app.route('/send_sms', methods=['GET', 'POST'])
@roles_accepted('Admin')
def send_sms():
form = SendMailForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
recipients = db.session.query(User.mobile).all()
list_recipients = filter(None, list(zip(*recipients)[0]))
send_sms.delay(list_recipients, form.text.data)
return render_template('send_generic.html', form=form, send_sms=send_sms)
我的send_sms函数已经被celery注册为任务:
(env)RP:secTrial Rob$ celery inspect registered
-> celery@RP.local: OK
* app.send_email
* app.send_security_email
* app.send_sms
在配置方面,我只是使用了guest:rabbitmq。
CELERY_BROKER_URL = 'amqp://guest@localhost//'
CELERY_RESULT_BACKEND = 'amqp://guest@localhost//'
1 个回答
26
视图的名字 send_sms
和 celery 任务的名字冲突了。当你在包含这个视图的模块中使用 send_sms
时,它指的是视图,而不是任务。
为了避免覆盖,建议使用不同的名字。