我知道有關於如何處理燒瓶幾個問題「工作的應用程序上下文之外」,但我不能讓他們爲我工作瓶0.10蒙戈工作應用程序上下文之外
我有一個長期運行mongo聚合查詢,並計劃使用apscheduler定期運行。 以下是我的應用程序結構,但任務因「RuntimeError:在應用程序上下文之外工作」而失敗。 ihttp://flask.pocoo.org/docs/patterns/sqlite3/有一些使用新燒瓶的例子,但是想知道是否有人可以建議如何在全球範圍內正確保存mongodb連接並在apscheduler中共享該連接
__init.py__
from app import create_app
app.py
from flask import Flask, request, render_template,g
from .extention import mongo, redis, sched
def create_app(config=None):
"""Create a Flask app."""
app = Flask(__name__)
configure_extensions(app)
return app
def configure_extensions(app):
mongo.init_app(app) # initialise mongo connection from the config
redis.init_app(app)
from schedule_tasks import *
extention.py
from flask.ext.pymongo import PyMongo
mongo = PyMongo()
from apscheduler.scheduler import Scheduler
config = {'apscheduler.jobstores.file.class': 'apscheduler.jobstores.shelve_store:ShelveJobStore',
'apscheduler.jobstores.file.path': '/tmp/sched_dbfile'}
sched = Scheduler(config)
from flask.ext.redis import Redis
redis = Redis()
schedule_tasks.py
from .extention import mongo
@sched.interval_schedule(minutes=1)
def long_running_queries():
## mongo agg query ##
mongo.db.command("aggregate", "collection", pipeline = "some query")
sched.start()
sched.print_jobs()
究竟是哪一行拋出了RuntimeError?你可以把一個完整的堆棧跟蹤? –
我得到的錯誤是「引發RuntimeError('在應用程序上下文外部工作')」文件「schedule_tasks.py」,第5行,在mongo – Linus