Commit 9c034dd25378ce5caea646f0c248e18a009a89f1
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 | 2 | import shelve |
2 | 3 | from os.path import join, isfile |
3 | 4 | |
4 | -import enum | |
5 | 5 | from flask_admin.contrib.sqla import ModelView |
6 | 6 | from flask_login import UserMixin, AnonymousUserMixin |
7 | 7 | from flask_sqlalchemy import SQLAlchemy |
... | ... | @@ -123,7 +123,11 @@ class Estimation(db.Model): |
123 | 123 | def set_output_dict(self, output): |
124 | 124 | # with shelve.open(filename=self.get_output_filename(), protocol=2) as shelf: |
125 | 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 | 131 | shelf['output'] = output |
128 | 132 | shelf.close() |
129 | 133 | |
... | ... | @@ -134,13 +138,22 @@ class Estimation(db.Model): |
134 | 138 | if self.output_yaml is None: |
135 | 139 | output_filename = self.get_output_filename() |
136 | 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 | 153 | self._output_dict = shelf['output'] |
142 | - # self._output_dict = copy(shelf['output']) | |
143 | 154 | shelf.close() |
155 | + | |
156 | + # mutex.release() | |
144 | 157 | else: |
145 | 158 | self._output_dict = None |
146 | 159 | else: | ... | ... |