Commit f48f4a8f6cdfc9950386687bf08daf27a5c33ac0

Authored by Antoine Goutenoir
1 parent 3aee1215
Exists in master

Optimize a bottleneck. 24000ms -> 544ms

Note that `with` statements for shelve are only supported in python 3.
README.md
... ... @@ -41,6 +41,17 @@ Then, source it to enable it.
41 41 cp .env.dist .env
42 42 nano .env
43 43  
  44 +### Configure permissions
  45 +
  46 +`var/runs` must be writeable by the application.
  47 +
  48 +
  49 +## Build CSS and JS ()for prod)
  50 +
  51 + flask assets build
  52 +
  53 +
  54 +
44 55  
45 56 ## Development
46 57  
... ... @@ -55,7 +66,3 @@ Then, visit http://localhost:5000
55 66 > We're trying to remove the need for the `export` statements, but…
56 67  
57 68  
58   -## Build CSS and JS for prod
59   -
60   - flask assets build
61   -
... ...
flaskr/controllers/main_controller.py
... ... @@ -575,12 +575,13 @@ def compute(): # process the queue of estimation requests
575 575 # WRITE RESULTS INTO THE DATABASE #########################################
576 576  
577 577 estimation.status = StatusEnum.success
578   - estimation.output_yaml = u"%s" % yaml_dump(results)
  578 + # estimation.output_yaml = u"%s" % yaml_dump(results)
  579 + estimation.set_output_dict(results)
579 580 db.session.commit()
580 581  
581 582 # FINALLY, RESPOND ########################################################
582 583  
583   - response += yaml_dump(results) + "\n"
  584 + # response += yaml_dump(results) + "\n"
584 585  
585 586 return _respond(response)
586 587  
... ... @@ -636,7 +637,8 @@ def consult_estimation(public_id, extension):
636 637 if estimation.status in unavailable_statuses:
637 638 abort(404)
638 639  
639   - return estimation.output_yaml
  640 + return u"%s" % yaml_dump(estimation.get_output_dict())
  641 + # return estimation.output_yaml
640 642  
641 643 elif 'csv' == extension:
642 644  
... ...
flaskr/models.py
  1 +import enum
  2 +import shelve
  3 +from os.path import join, isfile
1 4 from flask_admin.contrib.sqla import ModelView
2 5  
3 6 from flaskr.core import generate_unique_id, models
... ... @@ -5,7 +8,10 @@ from flask_sqlalchemy import SQLAlchemy
5 8 from flask_login import UserMixin, AnonymousUserMixin
6 9 from werkzeug.security import generate_password_hash, check_password_hash
7 10 from yaml import safe_load as yaml_load
8   -import enum
  11 +
  12 +from content import get_path
  13 +
  14 +
9 15  
10 16 # These are not the emission "models" in the scientific meaning of the word.
11 17 # They are the SQL Database Models.
... ... @@ -73,15 +79,36 @@ class Estimation(db.Model):
73 79 return self.run_name
74 80 return self.public_id
75 81  
  82 + def get_output_filename(self):
  83 + runs_dir = get_path("var/runs")
  84 + return join(runs_dir, self.public_id)
  85 +
  86 + def set_output_dict(self, output):
  87 + # with shelve.open(filename=self.get_output_filename(), protocol=2) as shelf:
  88 + # shelf['output'] = output
  89 + shelf = shelve.open(filename=self.get_output_filename(), protocol=2)
  90 + shelf['output'] = output
  91 + shelf.close()
  92 +
76 93 _output_dict = None
77 94  
78 95 def get_output_dict(self):
79 96 if self._output_dict is None:
80 97 if self.output_yaml is None:
81   - self._output_dict = None
  98 + output_filename = self.get_output_filename()
  99 + if isfile(output_filename):
  100 + # with shelve.open(filename=output_filename,
  101 + # protocol=2) as shelf:
  102 + # self._output_dict = shelf['output']
  103 + shelf = shelve.open(filename=output_filename, protocol=2)
  104 + self._output_dict = shelf['output']
  105 + # self._output_dict = copy(shelf['output'])
  106 + shelf.close()
  107 + else:
  108 + self._output_dict = None
82 109 else:
83 110 self._output_dict = yaml_load(self.output_yaml)
84   - return self._output_dict
  111 + return self._output_dict
85 112  
86 113 def is_one_to_one(self):
87 114 return self.scenario == ScenarioEnum.one_to_one
... ...