与Python中的flatMap等价';s的并发框架
我有一段代码如下:与Python中的flatMap等价';s的并发框架,python,parallel-processing,Python,Parallel Processing,我有一段代码如下: for x in range(10): for v in f(x): print(v) >>> sentences = ["a word is a word", "all words are words"] >>> (Stream(sentences) ... .flatmap(lambda sentence: sentence.split()) ... .chunk_by_
for x in range(10):
for v in f(x):
print(v)
>>> sentences = ["a word is a word", "all words are words"]
>>> (Stream(sentences)
... .flatmap(lambda sentence: sentence.split())
... .chunk_by_key(lambda x: hash(x) % 10)
... .reduce_once(lambda chunk: len(set(chunk)))
... .sum())
6
我想将它并行化,所以我可能会这样做
ex = ProcessPollExecutor()
for vs in ex.map(f, range(10)):
for v in vs:
print(v)
但是,f
是一个生成器,因此上面的代码实际上不起作用。
我可以更改f
以返回列表,但此列表太大,无法放入内存
理想情况下,我想要pyspark中的flatMap
。
但是,直接使用pyspark,比如sc.parallelize(范围(10)).flatMap(f).tolocaterator()
似乎不起作用。至少我不能让它在初始列表如此短的情况下使用多个处理器。
(我把所有的东西都试过了,但运气不好。)
我可能可以自己使用队列来运行一些东西,但我想知道是否有一种方法可以在Python并发框架中并行这些代码?我最终使用
多处理来编写我自己的小型库
它通过缓冲提供了相当高效的flapmap支持,并支持其他类似Spark的功能,如:
for x in range(10):
for v in f(x):
print(v)
>>> sentences = ["a word is a word", "all words are words"]
>>> (Stream(sentences)
... .flatmap(lambda sentence: sentence.split())
... .chunk_by_key(lambda x: hash(x) % 10)
... .reduce_once(lambda chunk: len(set(chunk)))
... .sum())
6