Python 使用APScheduler修改会话变量

Python 使用APScheduler修改会话变量,python,flask,apscheduler,Python,Flask,Apscheduler,我无法设置与会话变量交互的Apscheduler。我总是收到错误RuntimeError:在请求上下文之外工作。 下面代码的想法是有一个播放按钮,当按下该按钮时,会触发一个每3秒增加一周计数器的作业 我当前代码的部分内容: init.py app = Flask(__name__) scheduler = APScheduler() scheduler.init_app(app) scheduler.start() app.config.from_object(Config) routes.p

我无法设置与会话变量交互的Apscheduler。我总是收到错误
RuntimeError:在请求上下文之外工作。

下面代码的想法是有一个播放按钮,当按下该按钮时,会触发一个每3秒增加一周计数器的作业

我当前代码的部分内容:

init.py

app = Flask(__name__)
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
app.config.from_object(Config)
routes.py

@app.route('/play_btn', methods=['GET'])
def play_btn():
    print("play")
    scheduler.add_job(func=increment_week, trigger="interval", seconds=3, id="play_btn")
    return "nothing"

def increment_week():
    #with app.app_context():
    #    print(session(app))
    session['week'] += 1
    # print("Only printing is working fine")

incement\u week()
函数需要应用程序上下文来检索会话,这是有道理的。不幸的是,我的所有尝试都失败了,除非只是简单地打印静态字符串(这当然不需要上下文)。

实际上,您可以通过在play_btn函数中定义increment_week函数(并添加@copy_current_request_context decorator)来修改会话变量的值。但由于某些原因,当从外部访问此会话变量时,它不会更改。实际上,您可以通过在play_btn函数中定义increment_week函数(并添加@copy_current_request_context decorator)来修改会话变量的值。但由于某些原因,当从外部访问此会话变量时,它不会更改