Apscheduler在Flask中同时运行作业 [英] Apscheduler running jobs concurrently in Flask

查看:136
本文介绍了Apscheduler在Flask中同时运行作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Flask I'm attempting to run several jobs concurrently. But I'm faced with this issue:

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()

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

I'm creating multiple jobs by defining the following function:

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的内容,但仍然无法理解该概念.

Basically the function is adding jobs as a call to the function "chron" but args and id are different which creates unique jobs, running at intervals of five seconds. I get the app_context issue in the chron function. I have read about app_context but still unable to grasp the concept.

推荐答案

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

Your "chron" function is calling Flask current_app and jsonify() but it seems you haven't pushed a Flask context yet thus having this outside app context runtime error.

在调用上下文"功能之前,您需要推送Flask应用程序上下文.

Before you can call a "contexted" func, you need to push your Flask app context.

两种方法,

app = Flask(__name__)
# do some stuff on your app if you need
app.app_context().push()
# do some other stuff

或您的chron func中,

or in your chron func,

with app.app_context():
    # do something

您可以参考手动推送上下文了解更多详情

这篇关于Apscheduler在Flask中同时运行作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆