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

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

但是我收到这个错误,

Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Job "email_job_scheduling (trigger: cron[day_of_week='wed', hour='9', minute='57'], next run at: 2019-12-11 09:57:00)" raised an exception Traceback (most recent call last): File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/apscheduler/scheduler.py", line 512, in _run_job retval = job.func(*job.args, **job.kwargs) File "/home/abdellah/Documents/SUPPORT-STS/project/app/admin/views.py", line 29, in email_job_scheduling send_email(to,subject,message) File "/home/abdellah/Documents/SUPPORT-STS/project/app/emails.py", line 11, in send_email mail.send(msg) File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/flask_mail.py", line 491, in send with self.connect() as connection: File "/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-packages/werkzeug/local.py", line 348, in getattr return getattr(self._get_current_object(), name) File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/werkzeug/local.py", line 307, in _get_current_object return self.__local() File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/flask/globals.py", line 52, in _find_app raise RuntimeError(_app_ctx_err_msg) RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve this, set up an application context with app.app_context(). See the documentation for more information.

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

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

如果你 post 更多代码,我会更有帮助。

顺便想办法解决:

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)