Problem
1 2 3 4 5 6 7 8 9 10 11 12 13
| import numpy as np import multiprocessing as mp
class A: def __init__(self): lock = mp.Manager().Lock()
def save(self): np.save('./dict.npy', self.__dict__)
a_class = A() a_class.save() a_dict = np.load('./dict.npy')
|
1
| ValueError: Object arrays cannot be loaded when allow_pickle=False
|
However, if we try use a_dict = np.load('./dict.npy'), allow_pickle=True
.
1
| FileNotFoundError: [WinError 2] 系统找不到指定的文件。
|
Solution
mp.Manager().Lock()
can not be pickled. Though it works well when being saved, it cannot be loaded.
To Solve it, remove lock
from self.__dict__
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import numpy as np import multiprocessing as mp
class A: def __init__(self): self.abc = 'abc' self.lock = mp.Manager().Lock()
def save(self): save_dict = self.__dict__.copy() save_dict.pop('lock', None) np.save('./dict.npy', save_dict)
a_class = A() a_class.save() a_dict = np.load('./dict.npy')
|