Flask-Mail 和 Redis Queue 库集成出现错误 [英] Flask-Mail and Redis Queue library integration giving error

查看:56
本文介绍了Flask-Mail 和 Redis Queue 库集成出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Flask-Mail 扩展来启用应用程序中的邮件发送.我无法让 celery 与烧瓶一起工作,所以我查找了其他一些库并找到了 Redis 队列.

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.

代码:

     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'

当我运行代码时,rqworker 中的邮件发送失败,并出现以下错误:

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.

此错误的原因是什么?

没有属性发送是因为发送是一个实例方法.

No attribute send was due to send being an instance method.

现在有不同的问题.我收到错误消息,邮件对象没有任何属性 app.我猜 rq 的新进程不知道我的烧瓶应用程序,因此错误.如何解决这个问题?

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?

推荐答案

您试图将邮件对象实例的发送方法加入队列,而 RQ 无法将实例方法加入队列.如果您查看页面最底部的文档,它会提到这一点:

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'

这篇关于Flask-Mail 和 Redis Queue 库集成出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆