使用pickle时遇到TypeError: can't pickle _thread.RLock object

两次遇到这个问题,但是原因不同:

第一次是调用sklearn的包进行多线程训练时,需要传入一个参数是一个clallabe的函数,我把这个函数与训练的函数放到同一个类里,传入时用的是processor=self.my_processor,后面的my_porcessor是自己封装的方法,processor是要多线程训练的一个参数。

后面解决的方法是把my_processor函数封装到这个类的外面,processor=my_processor这样传入的。

第二次是使用pickle.dumps()时遇到这个问题的,需要dumps的对象是一个Config对象,但是这个对象中放入了Logger对象。然后在dumps的时候报错。

原因是:pickle不能序列化一些复杂对象。可以序列化的类型如下:
The following types can be pickled:

  • None, True, and False
  • integers, floating point numbers, complex numbers
  • strings, bytes, bytearrays
  • tuples, lists, sets, and dictionaries containing only picklable objects
  • functions defined at the top level of a module (using def, not lambda)
  • built-in functions defined at the top level of a module
  • classes that are defined at the top level of a module
  • instances of such classes whose dict or the result of calling getstate() is picklable (see section Pickling Class Instances for details).

参考:https://docs.python.org/3/library/pickle.html#pickle-inst

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值