I have been churning through the software development recently and have had some success bending celery to my will.
I have used it successfully to send emails, and have just tried to use almost exactly the same code (after restarting all processes etc) to send sms through Twilio.
However I keep getting the following issue:
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'
My code is as follows:
@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 when called from my views.py works perfectly, however send_sms.delay fails every time with the above error.
Any help on troubleshooting this is appreciated.
-- As requested:
@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', '[email protected]', 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)
My send_sms celery decorated function is showing up as a registered task:
(env)RP:secTrial Rob$ celery inspect registered
-> [email protected]: OK
* app.send_email
* app.send_security_email
* app.send_sms
and for config I am simply using the guest:rabbitmq
CELERY_BROKER_URL = 'amqp://guest@localhost//'
CELERY_RESULT_BACKEND = 'amqp://guest@localhost//'
The view name send_sms
conflicts with the celery task name. The name send_sms
references the view, not the task, when used in the module that contains the view.
Use different name to avoid the overwriting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With