问题
I am using Flask-Mail extension to enable mail sending in the app. I was not able to get celery working with flask so I looked up some other library and found Redis Queue.
Code:
from flask.ext.mail import Mail,Message
from rq import Queue
mail = Mail() # mail.init_app(app) is done in top app.py
q = Queue()
@mod.route('/test')
def m11():
msg = Message("Signup Successfull",
recipients=['abc@gmail.com'])
msg.body = "Hello there, Welcome!"
q.enqueue(mail.send, msg)
return 'done'
When I run the code then mail sending is failed in rqworker giving following error:
17:04:49: *** Listening on default...
17:06:08: default: flaskext.mail.send(<flaskext.mail.message.Message object at
0x1027a9750>) (8c86f0f9-ae76-4297-bf17-a171a67f1b44)
17:06:08: 'module' object has no attribute 'send'
17:06:08: Moving job to failed queue.
What can be the reason of this error?
Edit: No attribute send was due to send being an instance method.
There is different problem now. I receive error that Mail object do not have any attribute app. I guess new process of rq does not know about my flask app and hence the error. How to solve this problem?
回答1:
You're trying to enqueue the send method of the mail object instance and RQ can't enqueue instance methods. If you look at the documentation at the very bottom of the page it mentions this:
http://python-rq.org/docs/
Try defining another method and sending the mail that way. Such as...
from flask.ext.mail import Mail,Message
from rq import Queue
mail = Mail()
q = Queue()
def queue_mail(msg):
mail.send(msg)
@mod.route('/test')
def m11():
msg = Message("Signup Successfull",
recipients=['abc@gmail.com'])
msg.body = "Hello there, Welcome!"
q.enqueue(queue_mail, msg)
return 'done'
来源:https://stackoverflow.com/questions/11527413/flask-mail-and-redis-queue-library-integration-giving-error