RuntimeError:在应用程序上下文之外工作。在我的Flask应用程序中

2024-10-02 14:19:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用以下功能计划从我的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中声明app的方式,是否有任何关系,或者我必须在这个文件中添加schedule函数。你知道吗

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.


Tags: toinpyconfigapphomevenvemail
1条回答
网友
1楼 · 发布于 2024-10-02 14:19:51

是的,如果没有指定应用程序上下文,则应该创建它。 这是因为必须在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)

相关问题 更多 >