Python TypeError:无法pickle'_thread.RLock';对象并发期货
我试图使用并发期货来加速我的代码,并将字典保存为pickle文件,但我一直遇到一个错误。我同时使用了ThreadPoolExecutor和ProcessPoolExecutor,两者都导致错误类型错误:无法pickle'\u thread.RLock'对象。我没有找到解决方法,我只知道我无法保存/更改锁定的对象。我想知道为什么这个对象一开始就被锁定了,我怎样才能修复这个问题并保存我的字典以备以后在数据处理中使用。下面是导致错误的代码Python TypeError:无法pickle'_thread.RLock';对象并发期货,python,multithreading,multiprocessing,concurrent.futures,Python,Multithreading,Multiprocessing,Concurrent.futures,我试图使用并发期货来加速我的代码,并将字典保存为pickle文件,但我一直遇到一个错误。我同时使用了ThreadPoolExecutor和ProcessPoolExecutor,两者都导致错误类型错误:无法pickle'\u thread.RLock'对象。我没有找到解决方法,我只知道我无法保存/更改锁定的对象。我想知道为什么这个对象一开始就被锁定了,我怎样才能修复这个问题并保存我的字典以备以后在数据处理中使用。下面是导致错误的代码 import numpy as np import pickl
import numpy as np
import pickle
import imquality.brisque as brisque
import concurrent.futures
import os
import shutil
import time
# define parameters
nthreads = 4
electrodeNames = ['I_1', 'I_2']
otsu_brisque = {}
# electrodeNames = ['I_1', 'I_2', 'II_2', 'II_3', 'III_1', 'III_2', 'III_3', 'IV_1', 'IV_2', 'IV_3',
# 'Litarion_0', 'E35_0', '25R6_0', 'GCA400_0','GCA2000_0']
# otsu_brisque = {}
# OTSU Intervariance for greyscale images
def threshold_otsu(image, nbins=256):
if len(image.shape) > 2 and image.shape[-1] in (3, 4):
msg = "threshold_otsu is expected to work correctly only for " \
"grayscale images; image shape {0} looks like an RGB image"
warn(msg.format(image.shape))
# Check if the image is multi-colored or not
if image.min() == image.max():
raise ValueError("threshold_otsu is expected to work with images "
"having more than one color. The input image seems "
"to have just one color {0}.".format(image.min()))
hist, bin_edges = np.histogram(image, bins=nbins, range=None)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2.
hist = hist/float(hist.sum())
# class probabilities for all possible thresholds
weight1 = np.cumsum(hist)
weight2 = np.cumsum(hist[::-1])[::-1]
# class means for all possible thresholds
mean1 = np.cumsum(hist * bin_centers) / weight1
mean2 = (np.cumsum((hist * bin_centers)[::-1]) / weight2[::-1])[::-1]
# Clip ends to align class 1 and class 2 variables:
# The last value of ``weight1``/``mean1`` should pair with zero values in
# ``weight2``/``mean2``, which do not exist.
variance12 = weight1[:-1] * weight2[1:] * (mean1[:-1] - mean2[1:]) ** 2
idx = np.argmax(variance12)
interVar = max(variance12)
threshold = bin_centers[:-1][idx]
return threshold, interVar
def main():
with concurrent.futures.ThreadPoolExecutor() as executor:
for electrode_index, electrode in enumerate(electrodeNames):
otsu_brisque[electrode] = executor.submit(worker, electrode)
executor.shutdown(wait=True)
with open('./otsu_brisque.pkl', 'wb') as handle:
pickle.dump(otsu_brisque, handle, protocol=pickle.HIGHEST_PROTOCOL)
def worker(electrode):
print(f'Launching electrde {electrode}.')
gray = np.load(f'../training_data/{electrode}.npy')
new_arr = ((gray - gray.min()) * (1/(gray.max() - gray.min()) * 1)).astype('float64')
threshold, interVar = threshold_otsu(new_arr[:,:,:])
briskXY = np.zeros(gray.shape[0]); briskYZ = np.zeros(gray.shape[1]); briskXZ = np.zeros(gray.shape[2])
for i in range(5):
briskXY[i] = brisque.score(new_arr[i,:,:])
for i in range(10):
briskYZ[i] = brisque.score(new_arr[:,i,:])
briskXZ[i] = brisque.score(new_arr[:,:,i])
temp = {'otsu': interVar,
'brisqueXY': briskXY,
'brisqueYZ': briskYZ,
'brisqueXZ': briskXZ,
'brisqueXY': np.mean(briskXY),
'brisqueYZ': np.mean(briskYZ),
'brisqueXZ': np.mean(briskXZ),
'brisqueMEAN': np.mean([np.mean(briskXY), np.mean(briskYZ), np.mean(briskXZ)])}
return temp
if __name__ == "__main__":
main()
错误也在下面
Launching electrde I_2.
Traceback (most recent call last):
File "[filepath]", line 90, in <module>
main()
File "[filepath]", line 59, in main
pickle.dump(otsu_brisque, handle, protocol=pickle.HIGHEST_PROTOCOL)
TypeError: cannot pickle '_thread.RLock' object
启动electrde I_2。
回溯(最近一次呼叫最后一次):
文件“[filepath]”,第90行,在
main()
文件“[filepath]”,第59行,在main中
pickle.dump(otsu_-brisque,handle,protocol=pickle.HIGHEST_protocol)
TypeError:无法pickle“\u thread.RLock”对象