即使它似乎计算,Python dask程序也无法产生输出

时间:2016-04-13 23:31:27

标签: python python-2.7 parallel-processing python-multiprocessing dask

我很困惑为什么我的dask程序没有产生任何输出,它只是在提交后挂起。我已指定使用进程而不是线程,并且可以在提交时看到所有核心(如此处所示:dask computation not executing in parallel),因此它似乎计算但从未完成。我只是想在一个长文本文件列表上运行一个简单的正则表达式。我错过了一些明显的东西吗?

import re
from os import listdir

import dask.bag as db
import dask.multiprocessing
dask.set_options(get=dask.multiprocessing.get)


loc = 'D:\\...\\text_files\\'
txts = [loc + i for i in listdir(loc)[:10]]

#  Load data in parallel
f = db.from_filenames(txts)
f = f.repartition(3)

# Define the regex
regex = re.compile(r'\b[A-Z][a-z]+\b')

# create function to parallelize
def reg(text):
    return regex.findall(text)

# distribute the function over cores
output = f.map(reg).compute().concat()

1 个答案:

答案 0 :(得分:0)

两个建议:

  1. 将通话转至repartition。这将实例化数据,然后尝试在进程之间移动它,这通常很昂贵。您应该信任系统提供的默认值。它只会使用与核心一样多的进程。

  2. 仅在计算结束时调用.compute()。您可能想要交换以下两行:

    output = f.map(reg).compute().concat()  # before
    output = f.map(reg).concat().compute()  # after