Save non-pickleable variable?

MRAB python at mrabarnett.plus.com
Fri Oct 20 14:28:01 EDT 2017


On 2017-10-20 18:19, Israel Brewster wrote:
> tldr: I have an object that can't be picked. Is there any way to do a "raw" dump of the binary data to a file, and re-load it later?
> 
> Details: I am using a java (I know, I know - this is a python list. I'm not asking about the java - honest!) library (Jasper Reports) that I access from python using py4j (www.py4j.org <http://www.py4j.org/>). At one point in my code I call a java function which, after churning on some data in a database, returns an object (a jasper report object populated with the final report data) that I can use (via another java call) to display the results in a variety of formats (HTML, PDF, XLS, etc). At the time I get the object back, I use it to display the results in HTML format for quick display, but the user may or may not also want to get a PDF copy in the near future.
> 
> Since it can take some time to generate this object, and also since the data may change between when I do the HTML display and when the user requests a PDF (if they do at all), I would like to save this object for potential future re-use. Because it might be large, and there is actually a fairly good chance the user won't need it again, I'd like to save it in a temp file (tat would be deleted when the user logs out) rather than in memory. Unfortunately, since this is an object created by and returned from a java function, not a native python object, it is not able to be pickled (as the suggestion typically is), at least to my knowledge.
> 
> Given that, is there any way I can write out the "raw" binary data to a file, and read it back in later? Or some other way to be able to save this object? It is theoretically possible that I could do it on the java side, i.e. the library may have some way of writing out the file, but obviously I wouldn't expect anyone here to know anything about that - I'm just asking about the python side :-)
> 
As far as I can tell, what you're getting is a Python object that's a 
proxy to the actual Java object in the Java Virtual Machine. The Python 
side might be taking to the Java side via a socket or a pipe, and not 
know anything about, or have access to, the internals of the Java object.

In fact, you can't even be sure how a particular Python object is laid 
out in memory without reading the source code.



More information about the Python-list mailing list