I would like to create a class that describes a file resource and then pickle it. This part is straightforward. To be concrete, let’s say that I have a class “A” that has methods to operate on a file. I can pickle this object if it does not contain a file handle. I want to be able to create a file handle in order to access the resource described by “A”. If I have an “open()” method in class “A” that opens and stores the file handle for later use, then “A” is no longer pickleable. (I add here that opening the file includes some non-trivial indexing which cannot be cached–third party code–so closing and reopening when needed is not without expense). I could code class “A” as a factory that can generate file handles to the described file, but that could result in multiple file handles accessing the file contents simultaneously. I could use another class “B” to handle the opening of the file in class “A”, including locking, etc. I am probably overthinking this, but any hints would be appreciated.
Share
The question isn’t too clear; what it looks like is that:
Essentially, you want to make open files picklable. You can do this fairly easily, with certain caveats. Here’s an incomplete but functional sample:
Caveats and notes, some obvious, some less so:
open. If you’re using wrapper classes on files, likegzip.GzipFile, those should go above this, not below it. Logically, treat this as a decorator class on top offile.fileclass in Python 3. Even if you’re only using Python 2 right now, don’t subclassfile.I’d steer away from doing this; having pickled data dependent on external files not changing and staying in the same place is brittle. This makes it difficult to even relocate files, since your pickled data won’t make sense.