From c77bf75b2f41a790b885d4ee932438575ed65b70 Mon Sep 17 00:00:00 2001 From: osmala <ossi.laine@utu.fi> Date: Wed, 10 Jun 2020 12:52:50 +0300 Subject: [PATCH] Close db session manually after socket connection has ended --- app/__init__.py | 9 +++++---- app/experiment/views.py | 38 ++++++++++++++++--------------------- app/static/js/getCSV.js | 1 + app/static/js/getDrawing.js | 2 ++ embody_plot.py | 5 +++++ 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 0a1725a..71c6ee7 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -68,10 +68,6 @@ def get_locale(): """ -# Run flask app with socketIO -socketio = SocketIO(app, cors_allowed_origins="*") -# socketio = SocketIO() -socketio.init_app(app) #mariabd mysql portti 3306 tarkista? @@ -82,6 +78,11 @@ migrate = Migrate(app, db) login = LoginManager(app) login.login_view = 'login' +# Run flask app with socketIO +socketio = SocketIO(app, cors_allowed_origins="*") +# socketio = SocketIO() +socketio.init_app(app) + # Register blueprints from .task.views import task_blueprint from .experiment.views import experiment_blueprint diff --git a/app/experiment/views.py b/app/experiment/views.py index 46d148c..241ecb6 100644 --- a/app/experiment/views.py +++ b/app/experiment/views.py @@ -2,6 +2,7 @@ import os import secrets from datetime import date +from tempfile import mkstemp from flask_socketio import emit from sqlalchemy import and_ @@ -13,7 +14,8 @@ from flask import ( flash, redirect, url_for, - Blueprint + Blueprint, + send_file ) from app import app, db, socketio @@ -1045,7 +1047,7 @@ def remove_rows(rows): @socketio.on('connect', namespace="/create_embody") -def create_embody(): +def start_create_embody(): emit('success', {'connection': 'on'}) @@ -1059,47 +1061,39 @@ def create_embody(meta): @socketio.on('end', namespace="/create_embody") -def create_embody(): - emit('end', {'connection': 'off'}) - - +def end_create_embody(): + db.session.close() @socketio.on('connect', namespace="/download_csv") -def create_embody(): +def start_download_csv(): emit('success', {'connection': 'Start generating CSV file'}) - -from tempfile import mkstemp -from flask import send_file - @socketio.on('generate_csv', namespace="/download_csv") -def create_embody(meta): - +def process_download_csv(meta): exp_id = meta["exp_id"] data = generate_csv(exp_id) + # error handling if isinstance(data, Exception): emit('timeout', {'exc': str(data)}) return - filename = "experiment_{}_{}".format( - exp_id, date.today().strftime("%Y-%m-%d")) - + # create temporary file fd, path = mkstemp() - - print(fd) - print(path) - with os.fdopen(fd, 'w') as tmp: tmp.write(data) tmp.flush() + # return path and filename to front so user can start downloading + filename = "experiment_{}_{}".format( + exp_id, date.today().strftime("%Y-%m-%d")) path = path.split('/')[-1] - emit('file_ready', {'path': path, 'filename': filename}) - # return saved_data_as_file(filename, csv) +@socketio.on('end', namespace="/download_csv") +def end_download_csv(): + db.session.close() diff --git a/app/static/js/getCSV.js b/app/static/js/getCSV.js index f841602..9c20c76 100644 --- a/app/static/js/getCSV.js +++ b/app/static/js/getCSV.js @@ -26,6 +26,7 @@ $(document).ready(function() { socket.on('timeout', function(data) { // kill connection + socket.emit('end') socket.disconnect() exportButton.text('Export results') diff --git a/app/static/js/getDrawing.js b/app/static/js/getDrawing.js index 98d80c0..70db2ae 100644 --- a/app/static/js/getDrawing.js +++ b/app/static/js/getDrawing.js @@ -19,6 +19,8 @@ $(document).ready(function() { }); socket.on('end', function(img) { + // kill connection + socket.emit('end') socket.disconnect() // Draw image to statistic -page diff --git a/embody_plot.py b/embody_plot.py index 1c8666d..f540d77 100644 --- a/embody_plot.py +++ b/embody_plot.py @@ -124,6 +124,8 @@ def timeit(method): def get_coordinates(idpage, idembody=None, select_clause=SELECT_BY_PAGE_AND_PICTURE): """Select all drawn points from certain stimulus and plot them onto the human body""" + + # init db db = MyDB() db.query(select_clause, (idpage,idembody)) @@ -141,6 +143,9 @@ def get_coordinates(idpage, idembody=None, select_clause=SELECT_BY_PAGE_AND_PICT else: plt = plot_coordinates(coordinates, DEFAULT_IMAGE_PATH) + # close db connection + db.__del__() + # Save image to ./app/static/ img_filename = 'PAGE-' + str(idpage) + '-' + DATE_STRING + '.png' plt.savefig(STATIC_PATH + img_filename) -- GitLab