Debug - numpy.load()

Maple

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')
  • Title: Debug - numpy.load()
  • Author: Maple
  • Created at : 2024-12-18 14:01:00
  • Updated at : 2024-12-18 14:45:35
  • Link: https://www.maple367.eu.org/Code/Python/debug-numpy-load/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Debug - numpy.load()