Celery难度:函数对象没有属性延迟'

时间:2014-10-16 16:55:18

标签: python python-2.7 flask celery flask-security

我最近在软件开发过程中一直在努力,并且在我的意愿下弯曲芹菜取得了一些成功。

我已成功使用它发送电子邮件,并且刚尝试使用几乎完全相同的代码(在重新启动所有进程后等)通过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芹菜装饰功能显示为注册任务:

(env)RP:secTrial Rob$ celery inspect registered
-> celery@RP.local: OK
    * app.send_email
    * app.send_security_email
    * app.send_sms

对于配置我只是使用来宾:rabbitmq

CELERY_BROKER_URL = 'amqp://guest@localhost//'
CELERY_RESULT_BACKEND = 'amqp://guest@localhost//'

1 个答案:

答案 0 :(得分:14)

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

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