我编写了一个python代码,它将原始数据(STM显微镜)转换为png格式,并在我的Macbook Pro上完美地运行。
下面是简化的Python代码:
for root, dirs, file in os.walk(path):
for dir in dirs:
fpath = path +'/'+ dir
os.chdir(fpath)
spaths=savepath +'/'+ dir
if os.path.exists(spaths) ==False:
os.mkdir(spaths)
for files in glob.glob("*.sm4"):
for file in files:
data_conv (files, file, spaths)但它确实需要30-40分钟的for100文件。
现在,我想使用多线程技术(使用“并发未来”库)来减少处理时间。试图使用“PythonThinging教程”上的YouTube视频来修改python代码。
但是我必须在executor.map()方法中传递太多的参数,例如“root”、“dirs.”、“file”。我不知道如何进一步解决这个问题。
下面是简化的多线程Python代码
def raw_data (root, dirs, file):
for dir in dirs:
fpath = path +'/'+ dir
os.chdir(fpath)
spaths=savepath +'/'+ dir
if os.path.exists(spaths)==False:
os.mkdir(spaths)
for files in glob.glob("*.sm4"):
for file in files:
data_conv(files, file, spaths)
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(raw_data, root, dirs, file)NameError: name 'root' is not defined任何建议都很感谢,谢谢。
发布于 2021-08-27 15:30:02
谢谢你的建议,艾恩·谢文顿和德诺曼。
路径库确实减少了我代码中的杂乱。
"ProcessPoolExecutor“在我的CPU强大功能中工作。
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(raw_data, os.walk(path))https://stackoverflow.com/questions/68924479
复制相似问题