Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Apscheduler在Flask中同时运行作业_Python_Python 3.x_Flask_Apscheduler - Fatal编程技术网

Python Apscheduler在Flask中同时运行作业

Python Apscheduler在Flask中同时运行作业,python,python-3.x,flask,apscheduler,Python,Python 3.x,Flask,Apscheduler,在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 docume

在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函数中得到应用程序上下文问题。我已经阅读了有关应用程序上下文的内容,但仍然无法理解这个概念。

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

在调用“contexted”func之前,需要推送Flask应用程序上下文

有两种方式,

app = Flask(__name__)
# do some stuff on your app if you need
app.app_context().push()
# do some other stuff
或者在你的时间函数中

with app.app_context():
    # do something