TypeError: can't pickle Transaction objects 解决
在使用concurrent或进程池Pool进行并发操作时,遇到了这个错误
解决办法就是把map的接口修改为全局的。
from glob import glob
import os
import concurrent.futures
from multiprocessing import Pool
import lmdb
import time
class VerifyLabel:
def __init__(self):
self.file_name = ''
...
def check_txt(self, file_name):
print(f'file_name:{self.file_name}')
if __name__ == "__main__":
verify = VerifyLabel()
with concurrent.futures.ProcessPoolExecutor() as executor:
roots = glob('/root/images/*.jpg')
executor.map(verify.get_text, roots)
像上面的代码在运行时,就会报这个can’t pickle Transaction objects 错误。
map不能直接操作对象
改为:
from glob import glob
import os
import concurrent.futures
from multiprocessing import Pool
import lmdb
import time
A = None
class VerifyLabel:
def __init__(self):
self.file_name = ''
def check_txt(self, file_name):
print(f'file_name:{self.file_name}')
def check_txt( file_name):
global A
print(f'file_name:{A.file_name}')
if __name__ == "__main__":
verify = VerifyLabel()
A = verify
with concurrent.futures.ProcessPoolExecutor() as executor:
roots = glob('/root/images/*.jpg')
executor.map(verify.get_text, roots)
这样就不会错了。