Commit 9c034dd25378ce5caea646f0c248e18a009a89f1

Authored by Antoine Goutenoir
1 parent 197168fd
Exists in master

fix: tweak shelve usage to avoid `[Errno 11] Resource temporarily unavailable`

We may even add a proper mutex at some point, if that ain't enough.
(but since we only write once, it's probably overkill)
Showing 1 changed file with 20 additions and 7 deletions   Show diff stats
flaskr/models.py
  1 +import enum
1 import shelve 2 import shelve
2 from os.path import join, isfile 3 from os.path import join, isfile
3 4
4 -import enum  
5 from flask_admin.contrib.sqla import ModelView 5 from flask_admin.contrib.sqla import ModelView
6 from flask_login import UserMixin, AnonymousUserMixin 6 from flask_login import UserMixin, AnonymousUserMixin
7 from flask_sqlalchemy import SQLAlchemy 7 from flask_sqlalchemy import SQLAlchemy
@@ -123,7 +123,11 @@ class Estimation(db.Model): @@ -123,7 +123,11 @@ class Estimation(db.Model):
123 def set_output_dict(self, output): 123 def set_output_dict(self, output):
124 # with shelve.open(filename=self.get_output_filename(), protocol=2) as shelf: 124 # with shelve.open(filename=self.get_output_filename(), protocol=2) as shelf:
125 # shelf['output'] = output 125 # shelf['output'] = output
126 - shelf = shelve.open(filename=self.get_output_filename(), protocol=2) 126 + shelf = shelve.open(
  127 + filename=self.get_output_filename(),
  128 + flag='c', # read/write, create if needed
  129 + protocol=2
  130 + )
127 shelf['output'] = output 131 shelf['output'] = output
128 shelf.close() 132 shelf.close()
129 133
@@ -134,13 +138,22 @@ class Estimation(db.Model): @@ -134,13 +138,22 @@ class Estimation(db.Model):
134 if self.output_yaml is None: 138 if self.output_yaml is None:
135 output_filename = self.get_output_filename() 139 output_filename = self.get_output_filename()
136 if isfile(output_filename): 140 if isfile(output_filename):
137 - # with shelve.open(filename=output_filename,  
138 - # protocol=2) as shelf:  
139 - # self._output_dict = shelf['output']  
140 - shelf = shelve.open(filename=output_filename, protocol=2) 141 +
  142 + # Perhaps we'll need a mutex around here
  143 + # from threading import Lock
  144 + # mutex = Lock()
  145 + # mutex.acquire()
  146 +
  147 + # Not using the `with …` syntax, but we may in python3
  148 + shelf = shelve.open(
  149 + filename=output_filename,
  150 + flag='r',
  151 + protocol=2
  152 + )
141 self._output_dict = shelf['output'] 153 self._output_dict = shelf['output']
142 - # self._output_dict = copy(shelf['output'])  
143 shelf.close() 154 shelf.close()
  155 +
  156 + # mutex.release()
144 else: 157 else:
145 self._output_dict = None 158 self._output_dict = None
146 else: 159 else: