所以我在运行MongoDB的网络上有几台机器:
from pymongo import Connection c = Connection("10.130.10.12") some_data = c.MyData.MyCollection.find_one()
all_data = [] for server in my_list_of_servers: c = Connection(server) all_data.append(c.MyData.MyCollection.find_one())
答案 0 :(得分:2)
from multiprocessing import Pool
def connectAndCollect(server):
c = Connection(server)
return c.MyData.MyCollection.find_one()
pool = Pool(processes=10)
res = pool.map(connectAndCollect,servers)
map(lambda x: all_data.append(x),res)
pool.close()
多处理库专为此类任务而设计。如果您愿意,最终的map
调用可以替换为for循环。
此处描述了一般使用多处理模块进行Map / Reduce任务的说明: http://mikecvet.wordpress.com/2010/07/02/parallel-mapreduce-in-python/