如何腌制伐木机?
所从事的项目要求我能够在任何时候对容器对象进行 pickle,因为我们希望它在外部条件下经常失败,并且能够完全从我们中断的地方继续。
我非常广泛地使用 python 日志记录库,并且我的所有类都从设置一个记录器开始,如下所示:
class foo:
def __init__(self):
self.logger = logging.getLogger("package.foo")
由于我正在腌制一个容器类,因此它内部有多层类,每个类都有自己的记录器实例。
现在,由于某种原因,这些伐木者正在破坏 Pickle。我收到以下错误,如果我从所有类中删除 self.logger,该错误就会消失:
Can't pickle 'lock' object: <thread.lock object at ... >
所以我的问题是是否有某种方法可以从所有记录器中删除锁定对象,而不必递归整个对象树删除记录器,我必须在 unpickle 上重新创建它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还可以创建一个类来实现返回所需记录器的属性。从这个“LoggerMixin”继承的每个类现在都可以像以前使用它一样使用记录器。
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.
Python 3.7 中的新增功能 (bpo30520)
Logger
现在可以像许多其他对象一样进行 pickle 。New in Python 3.7 (bpo30520)
Logger
can now be pickled like many other objects.您可以创建一个包装记录器并实现 __getstate__ 的类,并且
__setstate__
。这是从 http://docs.python.org/library/pickle.html 粘贴的。
fh
的处理方式可能与您需要的类似。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.在这里找到了一个非常相似的问题,答案对我有用:
如何阻止属性在Python中被腌制
编辑:使用这个答案:如何在Python中阻止属性被pickle
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
您可以在这里使用
dill
,它可以 pickle 记录器和锁。You could have used
dill
here, which can pickle loggers and locks.