Skip to content
Snippets Groups Projects
Commit e658d2bb authored by Patrick Jentsch's avatar Patrick Jentsch
Browse files

Don't create new app instance for background tasks. Reuse the old one!

parent 565d273d
No related branches found
No related tags found
No related merge requests found
...@@ -13,16 +13,12 @@ mail = Mail() ...@@ -13,16 +13,12 @@ mail = Mail()
socketio = SocketIO() socketio = SocketIO()
def create_app(config_name, main=True): def create_app(config_name):
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(config[config_name]) app.config.from_object(config[config_name])
config[config_name].init_app(app) config[config_name].init_app(app)
db.init_app(app) db.init_app(app)
if not main:
return app
login_manager.init_app(app) login_manager.init_app(app)
mail.init_app(app) mail.init_app(app)
socketio.init_app(app, message_qeue='redis://') socketio.init_app(app, message_qeue='redis://')
......
from flask import request from flask import current_app, request
from flask_login import current_user, login_required from flask_login import current_user, login_required
from .. import create_app, db, socketio from .. import db, socketio
from ..models import User from ..models import User
import json import json
import jsonpatch import jsonpatch
...@@ -23,6 +23,7 @@ def connect(): ...@@ -23,6 +23,7 @@ def connect():
' task associated with the sid. ' task associated with the sid.
''' '''
socketio.start_background_task(background_task, socketio.start_background_task(background_task,
current_app._get_current_object(),
current_user.id, current_user.id,
request.sid) request.sid)
...@@ -37,7 +38,7 @@ def disconnect(): ...@@ -37,7 +38,7 @@ def disconnect():
disconnected.append(request.sid) disconnected.append(request.sid)
def background_task(user_id, session_id): def background_task(app, user_id, session_id):
''' '''
' Sends initial corpus and job lists to the client. Afterwards it checks ' Sends initial corpus and job lists to the client. Afterwards it checks
' every 3 seconds if changes to the initial values appeared. If changes are ' every 3 seconds if changes to the initial values appeared. If changes are
...@@ -48,12 +49,6 @@ def background_task(user_id, session_id): ...@@ -48,12 +49,6 @@ def background_task(user_id, session_id):
' '
' > where '*' is either 'corpora' or 'jobs' ' > where '*' is either 'corpora' or 'jobs'
''' '''
'''
' Create an app instance to get access to an app_context with which db
' operations are fulfilled.
'''
app = create_app(os.getenv('FLASK_CONFIG') or 'default', main=False)
with app.app_context(): with app.app_context():
user = db.session.query(User).filter_by(id=user_id).first() user = db.session.query(User).filter_by(id=user_id).first()
''' Get current values from the database. ''' ''' Get current values from the database. '''
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment