【发布时间】:2021-08-25 14:10:11
【问题描述】:
我编写了一个 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)
但 100 个文件确实需要 30 - 40 分钟。
现在,我想使用多线程技术(使用“并发未来”库)减少处理时间。以“Python 线程教程”的 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
感谢您的任何建议。
【问题讨论】:
-
如果工作负载受 CPU 限制,您应该改用
concurrent.futures.ProcessPoolExecutor,因为由于 GIL,Python 线程不会同时运行。您是否需要用for root, dirs, file in os.walk(path):结束对executor.map的呼叫? -
对不起,我不是这里的专家,我不知道什么是 GIL。但是,我需要通过多线程或多处理来减少处理时间。 ............. {您是否需要将您对 executor.map 的调用包装为 os.walk(path) 中的 root、dirs、文件:?} YES
-
除非您受 IO 限制(大量网络/API 调用、写入/读取文件),否则多处理是您最好的选择。 GIL 防止线程并发(同时)运行
-
任何示例或建议都有助于理解实现代码。
标签: python python-3.x multithreading python-multithreading concurrent.futures