使用芹菜的困难:函数对象没有属性'delay'



我最近一直在努力进行软件开发,并取得了一些成功。

我已经成功地使用它发送电子邮件,并且刚刚尝试使用几乎完全相同的代码(在重新启动所有进程等之后)通过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)

send_email.delay在从我的views.py调用时工作得很好,但是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芹菜装饰函数显示为一个注册任务:

(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//'

视图名称send_sms与芹菜任务名称冲突。在包含视图的模块中使用名称send_sms时,它引用的是视图,而不是任务。

使用不同的名称以避免覆盖。

相关内容

  • 没有找到相关文章

最新更新