gpt4 book ai didi

python - 我的 Flask 应用程序中出现 RuntimeError : Working outside of application context.

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:25 26 4
gpt4 key购买 nike

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

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/

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

我会尝试顺便找到解决方案:

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)

关于python - 我的 Flask 应用程序中出现 RuntimeError : Working outside of application context.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59160912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com