繁体   English   中英

运行时错误:在应用程序上下文之外工作。 在我的烧瓶应用程序中

[英]RuntimeError: Working outside of application context. in my flask app

我正在尝试使用此功能安排从我的烧瓶应用程序发送电子邮件:

from apscheduler.scheduler import Scheduler
scheduler = Scheduler()
scheduler.start()

def email_job_scheduling():
    to="abdellah.ala@gmail.com"
    subject="summary projects"
    message="your summary projects"
    send_email(to,subject,message)

scheduler.add_cron_job(email_job_scheduling, day_of_week='tue', hour=12, minute=55)

这就是我在文件init .py 中声明应用程序的方式,是否有任何关系,或者我必须在此文件中添加调度功能。

login_manager = LoginManager()
db = SQLAlchemy()
mail = Mail()

def create_app(config_name):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')
    app.permanent_session_lifetime = timedelta(minutes=10)

    db.init_app(app)
    mail.init_app(app)
    login_manager.init_app(app)
    return app

但我收到此错误,

调试模式:关闭 * 在http://127.0.0.1:5000/ 上运行(按 CTRL+C 退出) Job "email_job_scheduling (trigger: cron[day_of_week='wed', hour='9', minute='57' ], next run at: 2019-12-11 09:57:00)”引发异常回溯(最近一次调用最后一次):文件“/home/abdellah/Documents/venv/lib64/python3.6/site-packages/ apscheduler/scheduler.py”,第 512 行,在 _run_job retval = job.func(*job.args, **job.kwargs) 文件“/home/abdellah/Documents/SUPPORT-STS/project/app/admin/views. py”,第 29 行,在 email_job_scheduling send_email(to,subject,message) 文件“/home/abdellah/Documents/SUPPORT-STS/project/app/emails.py”,第 11 行,在 send_email mail.send(msg) 文件中“/home/abdellah/Documents/venv/lib64/python3.6/site-packages/flask_mail.py”,第 491 行,以 self.connect() 作为连接发送:文件“/home/abdellah/Documents/venv/ lib64/python3.6/site-packages/flask_mail.py", line 508, in connect return Connection(app.extensions['mail']) File "/home/abdellah/Documents/venv/lib64/python3.6/site - 包/werkze ug/local.py”,第 348 行,在getattr 中返回 getattr(self._get_current_object(), name) 文件“/home/abdellah/Documents/venv/lib64/python3.6/site-packages/werkzeug/local.py” ,第 307 行,在 _get_current_object 中返回 self.__local() 文件“/home/abdellah/Documents/venv/lib64/python3.6/site-packages/flask/globals.py”,第 52 行,在 _find_app 中引发 RuntimeError(_app_ctx_err_msg)运行时错误:在应用程序上下文之外工作。

这通常意味着您尝试使用需要以某种方式与当前应用程序对象交互的功能。 要解决此问题,请使用 app.app_context() 设置应用程序上下文。 有关更多信息,请参阅文档。

是的,如果您尚未指定应用程序上下文,则应创建它。 这是因为有必要在 Flask 应用程序中定义所有必要的资源。 该文档完美地解释了在哪些情况下以及如何在 Flask 中使用上下文应用程序。

https://flask.palletsprojects.com/en/1.0.x/appcontext/

如果您发布更多代码,我可能会更有帮助。

我会尽量找到解决办法:

from flask import g

def email_job_scheduling():
    a = "abdellah.ala@gmail.com"
    subject = "summary projects"
    message = "your summary projects"
    send_email (to, subject, message)

def get_scheduler():
    if "scheduler" is not in g:
    g.scheduler = Scheduler()
    g.scheduler.start()
    returns g.scheduler

with app.app_context(): #add the necessary resources
    scheduler = get_scheduler()
    #add another code if you need to set another resource

#This piece of code I think is in the main
scheduler.add_cron_job (email_job_scheduling, day_of_week = 'tue', now = 12, minute = 55)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM