使用pool.map进行Python多线程处理

时间:2017-06-15 14:01:40

标签: python multithreading python-multithreading python-multiprocessing

我正在尝试使用时间维度为NetCDF文件中的每个变量创建GIF。

filepath = 'foam.nc'
variables = getVariables(filepath)

lat = getValue('lat', variables)[:]
lon = getValue('lon', variables)[:]

for var in keys:
    createGif(entity, lat, lon, t)

这样可行,但这个过程需要很长时间(250秒)。此NetCDF文件包含4个维度(时间,混合,纬度,长度),时间有9个深度。

使用以下示例:https://stackoverflow.com/a/28463266/8166528https://stackoverflow.com/a/28975239/8166528,我试图通过为每个“createGif”提供一个不同的线程来加速使用pool.map的过程,但我无法让它工作

我尝试了什么:

entities = []
lats = []
lons = []
ts = []
for var in keys:
    entity = getValue(var, variables)
    entities.append(entity)
    lats.append(lat)
    lons.append(lon)
    ts.append(t)

pool = ThreadPool(8)
results = pool.starmap(createGif, zip(entities, lats, lons, ts))
pool.close()
pool.join()

有人能指出我正确的方向吗?

0 个答案:

没有答案
相关问题