【发布时间】:2016-08-06 18:19:57
【问题描述】:
我有以下程序:
import string
import itertools
import multiprocessing as mp
def test(word_list):
return list(map(lambda xy: (xy[0], len(list(xy[1]))),
itertools.groupby(sorted(word_list))))
def f(x):
return (x[0], len(list(x[1])))
def test_parallel(word_list):
w = mp.cpu_count()
pool = mp.Pool(w)
return (pool.map(f, itertools.groupby(sorted(word_list))))
def main():
test_list = ["test", "test", "test", "this", "this", "that"]
print(test(test_list))
print(test_parallel(test_list))
return
if __name__ == "__main__":
main()
输出是:
[('test', 3), ('that', 1), ('this', 2)]
[('test', 0), ('that', 0), ('this', 1)]
第一行是预期的正确结果。我的问题是,为什么 pool.map() 不返回与 map() 相同的结果?
另外,我知道 6 项列表并不是多处理的完美案例。这只是我在大型应用程序中实现时遇到的问题的一个演示。
我正在使用 Python 3.5.1。
【问题讨论】:
标签: python group-by multiprocessing itertools pool