【问题标题】:Running flask app with celery and rabbitmq使用 celery 和 rabbitmq 运行烧瓶应用程序
【发布时间】:2021-03-09 08:18:25
【问题描述】:

我的 Flask 项目具有以下文件结构

flask_app/
    my_app/
        - __init__.py
        auth/
            - __init__.py
            - emailing.py
            - flask_celery 
            - models.py
            - views.py
        static/
            - css/
            - js/
        templates/
            - base.html
            - home.html
            - login.html
            - first_pdf.html 
    - run.py

在文件 emailing.py 中

from flask_mail import Message, Mail
from my_app import app
from my_app.auth.flask_celery import make_celery
from flask import render_template
import pdfkit
from my_app.auth.models import User

app.config["CELERY_BROKER_URL"] = "amqp://localhost:5672//"
celery = make_celery(app)
mail = Mail(app)


def sending_first_email(name, last_name):
    first_task.delay(name, last_name)
    return "Generated pdf and sent"

@celery.task(name="views.first_task")
def first_task(name, last_name):
    rendered = render_template("first_pdf.html", name=name, last_name=last_name)

    employee = User.query.filter_by(full_name=name + " " + last_name).first()
    pdf = pdfkit.from_string(rendered, False)
    msg = Message("New oncoming employee", sender="help@abc.ac.za", 
                  recipients= [employee.manager_email_address])

    msg.attach("employee.pdf", "application/pdf", pdf)
    msg.body = "Please find attached the details of the employee"
    mail.send(msg)
    return "First Message has been sent"

然后我们在 flask_celery.py 上设置 celery。我的问题现在是运行应用程序。我首先启动rabbitmq服务器,然后在项目根文件夹“flask_app”上尝试以下命令

celery -A emailing.celery worker --loglevel=info

我收到一个错误“错误:'-A' / '--app' 的值无效:没有名为 'emailing' 的模块” " 并且在此目录 'flask_app/my_app/auth' 上运行相同的命令时,我收到以下错误 “错误:'-A' / '--app' 的值无效:没有名为 'my_app' 的模块”。能否请我帮忙解决这个问题?

【问题讨论】:

    标签: python flask rabbitmq celery


    【解决方案1】:

    您可以在目录 flask_app/my_app/auth 的文件夹中运行 celery -A emailing worker --loglevel=info -Q views 希望能帮到你!

    【讨论】:

    • 不幸的是它仍然给出错误“错误:'-A'/'--app'的值无效:没有名为'my_app'的模块”。感谢您的回复
    猜你喜欢
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 2018-12-13
    • 2016-07-12
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多