雅各布·奥斯特加德 presented这个挑战:
Write a program that reads text from standard-input, and returns (prints) the total number of distinct words found in the text.
我们如何通过并行编程来应对这一挑战(最好是使用 Go,但用英文描述就足够了)?
最佳答案
有几种可能性,但我猜你的意思是“高效”?
一般的想法是将文本拆分为可管理的 block ,将这些 block 放入队列中,并让多个消费者处理这些 block 。
对我来说,这看起来像是一个典型的 Map/Reduce 应用程序:
_ Worker_
/ \
/ \
Splitter--- Worker ---Aggregator
\ /
\_ Worker _/
理想情况下,“多个”队列应该是一个具有多个消费者的队列,这样即使一个工作人员放慢速度,整个过程也不会减慢太多。
我还会使用从 Splitter 到 Workers 的信号,让他们知道输入已完全消耗,他们可以开始将结果发送到聚合器。
关于language-agnostic - Go中的并行不同字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6436202/