烧瓶中同时运行作业

问题描述 投票:0回答:1

在Flask中,我尝试同时运行多个作业。但是我面临这个问题:

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.
Job "chron (trigger: interval[0:00:05], next run at: 2020-05-19 16:03:46 IST)" raised an exception
Traceback (most recent call last):
  File "C:\Users\mithi\AppData\Local\Programs\Python\Python37\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
    retval = job.func(*job.args, **job.kwargs)
  File "c:\Users\mithi\Desktop\appengineering\server.py", line 128, in chron
    return jsonify({})
  File "C:\Users\mithi\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\json\__init__.py", line 358, in jsonify
    if current_app.config["JSONIFY_PRETTYPRINT_REGULAR"] or current_app.debug:
  File "C:\Users\mithi\AppData\Local\Programs\Python\Python37\lib\site-packages\werkzeug\local.py", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Users\mithi\AppData\Local\Programs\Python\Python37\lib\site-packages\werkzeug\local.py", line 307, in _get_current_object
    return self.__local()
  File "C:\Users\mithi\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\globals.py", line 52, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

这是我的预定对象:

job_defaults = {
    'coalesce': False,
    'max_instances': 100
}

sched = BackgroundScheduler(daemon=True, job_defaults=job_defaults)
sched.start()

我通过定义以下功能来创建多个作业:

def etl():

    # read app config
    with open('appConfig.json', encoding="utf-8") as json_file:
        configData = json.load(json_file)

    for obj in configData:
        sched.add_job(chron, 'interval', seconds=5, args=[obj], id=obj)

基本上,该函数将作业添加为对函数“ chron”的调用,但是args和id不同,这会创建唯一的作业,每隔五秒钟运行一次。我在chron函数中遇到了app_context问题。我已经阅读了有关app_context的内容,但仍然无法理解该概念。

python python-3.x flask apscheduler
1个回答
0
投票

您的“ chron”函数正在调用Flask current_app和jsonify(),但似乎您尚未推送Flask上下文,因此出现了此外部应用程序上下文运行时错误。

© www.soinside.com 2019 - 2024. All rights reserved.