Working on a project that requires that I am able to pickle the container object at any point, since we expect it to fail on external conditions quite frequently and be able to fully pick up where we left off.
I'm using the python logging library quite extensively, and all of my classes start by setting up a logger like:
class foo:
def __init__(self):
self.logger = logging.getLogger("package.foo")
Since I'm pickling a container class, it has several layers of classes within it, each with their own logger instance.
Now, for some reason, these loggers are breaking Pickle. I'm getting the following error, which goes away if I delete self.logger from all the classes:
Can't pickle 'lock' object: <thread.lock object at ... >
So my question is whether or not there is some way to remove the lock objects from all the loggers without having to recurse through my whole object tree deleting loggers which I will have to recreate on unpickle.
New in Python 3.7 (bpo30520)
Logger
can now be pickled like many other objects.You could have used
dill
here, which can pickle loggers and locks.Found a very similar question here, with an answer that worked for me:
How to stop attributes from being pickled in Python
edit: used this answer: How to stop attributes from being pickled in Python
You could create a class that wraps the logger and implements
__getstate__
and__setstate__
.This is pasted from http://docs.python.org/library/pickle.html. The
fh
is handled in a way which may be similar to what you need.You could also create a class that implements a property which returns the needed logger. Every class which inherits from this "LoggerMixin" is now able to use the logger in the same way you were using it before.