gpt4 book ai didi

python - 如何使用 mincemeat 将 example.py 中定义的任务分发到两台客户端计算机?

转载 作者:行者123 更新时间:2023-11-28 17:48:52 24 4
gpt4 key购买 nike

我已经从 https://github.com/michaelfairley/mincemeatpy/zipball/v0.1.2 下载了带有示例的 mincemeat.py

example.py如下:

#!/usr/bin/env python
import mincemeat

data = ["Humpty Dumpty sat on a wall",
"Humpty Dumpty had a great fall",
"All the King's horses and all the King's men",
"Couldn't put Humpty together again",
]

datasource = dict(enumerate(data))

def mapfn(k, v):
for w in v.split():
yield w, 1

def reducefn(k, vs):
result = sum(vs)
return result

s = mincemeat.Server()
s.datasource = datasource
s.mapfn = mapfn
s.reducefn = reducefn

results = s.run_server(password="changeme")
print results

它用于字数统计程序。

我已经通过局域网连接了两台计算机。我使用一台计算机作为服务器并在其上运行 example.py;在作为客户端的第二台计算机上,我使用以下命令行语句运行 mincemeat.py:

python mincemeat.py -p changeme server-IP

它工作正常。

现在我已经通过路由器将局域网内的3台电脑连接起来了。然后一台机器作为服务器,我想在上面运行example.py,剩下的两台机器作为客户端机器运行。

我想将任务分配到我的两台客户端机器上。那么将map和reduce的任务分发给两台电脑的流程是怎样的呢?如何将 example.py 中定义的任务分配给分别具有唯一 IP 的两台客户端计算机?

最佳答案

默认示例几乎不包含 50 个单词。因此,当您切换窗口以启动第二个客户端时,第一个客户端已完成对文本的处理。相反,使用大文本文件运行相同的文件,您可以添加第二个客户端。下面应该工作。我用的是小说的纯文本格式Ulyesses (~1.5 MB) 来自 Project Gutenberg 这个例子。

在我的机器(Intel Xeon@ 3.10 GHz)中,2 个客户端用时不到 30 秒。因此,使用更大的文件或文件列表或快速启动第二个客户端。

#!/usr/bin/env python
import mincemeat

def file_contents(file_name):
f = open(file_name)
try:
return f.read()
finally:
f.close()

novel_name = 'Ulysses.txt'

# The data source can be any dictionary-like object
datasource = {novel_name:file_contents(novel_name)}

def mapfn(k, v):
for w in v.split():
yield w, 1

def reducefn(k, vs):
result = sum(vs)
return result

s = mincemeat.Server()
s.datasource = datasource
s.mapfn = mapfn
s.reducefn = reducefn

results = s.run_server(password="changeme")
print results

对于文件目录,请使用以下示例。将所有文本文件转储到文件夹 textfiles 中。

#!/usr/bin/env python
import mincemeat
import glob

all_files = glob.glob('textfiles/*.txt')

def file_contents(file_name):
f = open(file_name)
try:
return f.read()
finally:
f.close()

# The data source can be any dictionary-like object
datasource = dict((file_name, file_contents(file_name))
for file_name in all_files)

def mapfn(k, v):
for w in v.split():
yield w, 1

def reducefn(k, vs):
result = sum(vs)
return result

s = mincemeat.Server()
s.datasource = datasource
s.mapfn = mapfn
s.reducefn = reducefn

results = s.run_server(password="changeme")
print results

关于python - 如何使用 mincemeat 将 example.py 中定义的任务分发到两台客户端计算机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com