Ich speichere einige große Daten mit Pythons Beizmodul. Um genauer zu sein ich zwei Objekte auf die gleiche Gurke-Datei am Dumping wie folgt:pickle erhöht EOFError auf Ubuntu 14.04, funktioniert aber auf Mac OS und Linux Mint
def save(sim):
if sim.tstamp > 0:
with open(filepath(sim.identifier), 'wb') as f:
pickle.dump(sim.serialize(), f)
pickle.dump(sim, f)
else:
raise ValueError("Simulation not yet simulated")
sim
ist eine selbstgeschriebene Klasse, die verschiedene Attribute und ein Verfahren serialize()
genannt hat. Diese Methode konvertiert alle Attribute und ihre Werte zu einem Wörterbuch nach wie etwas mit:
def serialize(self):
keys = ['height', 'diameter', 'rpm', 'packaging']
serialized = dict((key, getattr(self, key)) for key in keys)
serialized['sectors'] = [sector.serialize() for sector in self.sectors]
return serialized
Also im Grunde sim.serialize()
gibt einen Wörterbuch, das in die Beize-Datei als erstes Objekt geworfen wird. Das zweite Objekt ist das Sim-Objekt selbst. Dies liegt an der großen Größe jedes Sim-Objekts. Die Bereitstellung des Wörterbuchs ermöglicht eine Art Zusammenfassung, um zu verhindern, dass die vollständige Simulation geladen wird, wenn dies nicht wirklich notwendig ist.
def load(fname, params_only=False):
simulation = find(fname) # find returns file name
try:
with open(simulation, 'r') as json_file:
return json.load(json_file)
except (UnicodeDecodeError, ValueError) as e:
with open(simulation, 'rb') as pickle_file:
params = pickle.load(pickle_file)
if params_only:
return params
return pickle.load(pickle_file)
Da eine ältere API von JSON-Dateien als auch fähig ist, ich brauche beide JSON- und Gurke-Dateien zu handhaben:
Um die Daten aus der Beize Datei Ich verwende zu laden.
Das Löschen der Objekte funktioniert wie gewünscht (es werden keine Fehlermeldungen ausgegeben). Wenn jedoch einige Daten anfordert Graphen, die die API, um die Daten unpickle benötigt, die die folgenden Fehler auslöst:
[2016-08-07 14:39:34,637] ERROR in app: Exception on /api/simulations/20160807_123707581/heatflux_distribution.png [GET]
Traceback (most recent call last):
File "/home/abkos/abkosproject/abkos/_tools.py", line 36, in load
return json.load(json_file)
File "/usr/lib/python3.4/json/__init__.py", line 265, in load
return loads(fp.read(),
File "/usr/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./app.py", line 118, in plot_figure
img = plotter(identifier, figname, request.args.items())
File "./app.py", line 122, in plotter
sim = Simulation.from_file(identifier)
File "/home/abkos/abkosproject/abkos/calculation.py", line 56, in from_file
params_or_simulation = load(fname, params_only=new)
File "/home/abkos/abkosproject/abkos/_tools.py", line 42, in load
return pickle.load(pickle_file)
_pickle.UnpicklingError
10.0.2.2 - - [07/Aug/2016 14:39:34] "GET /api/simulations/20160807_123707581/heatflux_distribution.png HTTP/1.1" 500 -
[2016-08-07 14:39:38,691] ERROR in app: Exception on /api/simulations/20160807_123707581/heatmap_axial_temperatures.png [GET]
Traceback (most recent call last):
File "/home/abkos/abkosproject/abkos/_tools.py", line 36, in load
return json.load(json_file)
File "/usr/lib/python3.4/json/__init__.py", line 265, in load
return loads(fp.read(),
File "/usr/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./app.py", line 118, in plot_figure
img = plotter(identifier, figname, request.args.items())
File "./app.py", line 122, in plotter
sim = Simulation.from_file(identifier)
File "/home/abkos/abkosproject/abkos/calculation.py", line 56, in from_file
params_or_simulation = load(fname, params_only=new)
File "/home/abkos/abkosproject/abkos/_tools.py", line 42, in load
return pickle.load(pickle_file)
EOFError
10.0.2.2 - - [07/Aug/2016 14:39:38] "GET /api/simulations/20160807_123707581/heatmap_axial_temperatures.png HTTP/1.1" 500 -
Der Code funktioniert gut auf MacOS und Linux Mint 3.4.3 Python. Es funktioniert jedoch nicht auf Ubuntu 14.04 (Python 3.4.3).
Haben Sie Ideen, wo Sie den Fehler untersuchen können?