简体   繁体   English

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

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

I'm trying to schedule sending email from my flask application with this function :我正在尝试使用此功能安排从我的烧瓶应用程序发送电子邮件:

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)

this is how i declare app in my file init .py, is there any relationship or must i add schedule function in this file.这就是我在文件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

but I receive this error,但我收到此错误,

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/werkze调试模式:关闭 * 在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", 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. 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)运行时错误:在应用程序上下文之外工作。

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().要解决此问题,请使用 app.app_context() 设置应用程序上下文。 See the documentation for more information.有关更多信息,请参阅文档。

Yes if you have not specified an application context you should create it.是的,如果您尚未指定应用程序上下文,则应创建它。 This is because it is necessary to define all the necessary resources within the Flask application.这是因为有必要在 Flask 应用程序中定义所有必要的资源。 The documentation explains perfectly in which cases and how you should use a context application in Flask.该文档完美地解释了在哪些情况下以及如何在 Flask 中使用上下文应用程序。

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

If you post more code I could be more helpful.如果您发布更多代码,我可能会更有帮助。

I will try to find a solution by the way:我会尽量找到解决办法:

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.

相关问题 运行时错误:在应用程序上下文之外工作。 烧瓶 - 邮件 - RuntimeError: Working outside of application context. Flask - Mail Flask `RuntimeError:在应用程序上下文之外工作。` 来自中间件 - Flask `RuntimeError: Working outside of application context.` from Middleware FlaskSocketIO - 运行时错误:在应用程序上下文之外工作。 要解决此问题,请使用 app.app_context() 设置应用程序上下文 - FlaskSocketIO - RuntimeError: Working outside of application context. To solve this, set up an application context with app.app_context() 如何修复“RuntimeError:在应用程序上下文之外工作。” 使用 Flask 创建蓝图时? - How to fix "RuntimeError: Working outside of application context." when creating blueprints with Flask? 运行时错误:在应用程序上下文之外工作。 与 app.app_context() 没有解决问题 - RuntimeError: Working outside of application context. with app.app_context() not resolving the issue Flask - RuntimeError:在应用程序上下文之外工作 - Flask - RuntimeError: Working outside of application context RuntimeError:在请求上下文之外工作。 含马里角 - RuntimeError: Working outside of request context. with gunicorn Flask RuntimeError:在应用程序上下文之外工作 - Flask RuntimeError: working outside of application context 在应用程序上下文之外工作。 (Python:Flask 错误!) - Working outside of application context. (Python: Flask error!) 单元测试会生成“RuntimeError:在应用程序上下文之外工作”。 使用模拟 4.0.0 - Unit tests generates “RuntimeError: Working outside of application context.” with Mock 4.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM