Python multiprocessing pool.map引发索引

2024-05-20 08:20:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用python/cython开发了一个实用程序,它可以对CSV文件进行排序并为客户端生成统计信息,但是调用pool.map似乎会在我的映射函数有机会执行之前引发异常。对少量文件进行排序似乎可以正常工作,但是随着文件数量增加到10个,我在调用pool.map之后会得到下面的索引器错误。有人碰巧认识到下面的错误吗?任何帮助都非常感谢。

虽然代码在NDA下,但是用例相当简单:

代码示例:

def sort_files(csv_files):
    pool_size = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=pool_size)
    sorted_dicts = pool.map(sort_file, csv_files, 1)
    return sorted_dicts

def sort_file(csv_file):
    print 'sorting %s...' % csv_file
    # sort code

输出:

File "generic.pyx", line 17, in generic.sort_files (/users/cyounker/.pyxbld/temp.linux-x86_64-2.7/pyrex/generic.c:1723)
    sorted_dicts = pool.map(sort_file, csv_files, 1)
  File "/usr/lib64/python2.7/multiprocessing/pool.py", line 227, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib64/python2.7/multiprocessing/pool.py", line 528, in get
    raise self._value
IndexError: list index out of range

Tags: 文件csvinmap排序linefilessort
2条回答

索引器错误是在sort_file()中的某个位置(即子进程中)出现的错误。它由父进程重新引发。显然multiprocessing并没有试图告诉我们错误的真正来源(例如发生在哪一行上),甚至没有试图告诉我们是什么参数导致了这个错误。我更讨厌multiprocessing了:-(

在命令输出中进一步检查。 至少在Python 3.4中,multiprocessing.pool将有助于在父进程回溯上方打印一个RemoteTraceback。你会看到这样的东西:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "/path/to/your/code/here.py", line 80, in sort_file
    something = row[index]
IndexError: list index out of range
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "generic.pyx", line 17, in generic.sort_files (/users/cyounker/.pyxbld/temp.linux-x86_64-2.7/pyrex/generic.c:1723)
    sorted_dicts = pool.map(sort_file, csv_files, 1)
  File "/usr/lib64/python2.7/multiprocessing/pool.py", line 227, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib64/python2.7/multiprocessing/pool.py", line 528, in get
    raise self._value
IndexError: list index out of range

在上述情况下,引发错误的代码位于/path/to/your/code/here.py", line 80

另见debugging errors in python multiprocessing

相关问题 更多 >