gpt4 book ai didi

具有分布式集群的 Python 多处理

转载 作者:IT老高 更新时间:2023-10-28 22:22:19 25 4
gpt4 key购买 nike

我正在寻找一个 python 包,它不仅可以在单台计算机内的不同内核上进行多处理,而且还可以在分布在多台机器上的集群中进行多处理。有很多不同的用于分布式计算的 Python 包,但大多数似乎都需要更改代码才能运行(例如,表示对象位于远程计算机上的前缀)。具体来说,我想要尽可能接近多处理 pool.map 函数的东西。因此,例如,如果在一台机器上,脚本是:

from multiprocessing import Pool
pool = Pool(processes = 8)
resultlist = pool.map(function, arglist)

那么分布式集群的伪代码将是:

from distprocess import Connect, Pool, Cluster

pool1 = Pool(processes = 8)
c = Connect(ipaddress)
pool2 = c.Pool(processes = 4)
cluster = Cluster([pool1, pool2])
resultlist = cluster.map(function, arglist)

最佳答案

如果您想要一个非常简单的解决方案,那么没有。

但是,有一个解决方案具有 multiprocessing 接口(interface) -- pathos -- 它能够通过并行映射建立与远程服务器的连接,并做多处理。

如果您想建立 ssh 隧道连接,您可以这样做……或者如果您可以使用不太安全的方法,您也可以这样做。

>>> # establish a ssh tunnel
>>> from pathos.core import connect
>>> tunnel = connect('remote.computer.com', port=1234)
>>> tunnel
Tunnel('-q -N -L55774:remote.computer.com:1234 remote.computer.com')
>>> tunnel._lport
55774
>>> tunnel._rport
1234
>>>
>>> # define some function to run in parallel
>>> def sleepy_squared(x):
... from time import sleep
... sleep(1.0)
... return x**2
...
>>> # build a pool of servers and execute the parallel map
>>> from pathos.pp import ParallelPythonPool as Pool
>>> p = Pool(8, servers=('localhost:55774',))
>>> p.servers
('localhost:55774',)
>>> y = p.map(sleepy_squared, x)
>>> y
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

或者,您可以配置为直接连接(无 ssh)

>>> p = Pool(8, servers=('remote.computer.com:5678',))
# use an asynchronous parallel map
>>> res = p.amap(sleepy_squared, x)
>>> res.get()
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

这有点挑剔,要让远程服务器工作,你必须事先在指定端口启动一个运行在 remote.computer.com 上的服务器——你必须确保本地主机和远程主机上的设置都将允许直接连接或 ssh 隧道连接。另外,您需要在每个主机上运行相同版本的 pathospppathos 分支。此外,对于 ssh,您需要运行 ssh-agent 以允许使用 ssh 进行无密码登录。

但是,希望一切正常……如果您的功能代码可以使用 dill.source.importable 传输到远程主机。

仅供引用,pathos 早就应该发布了,基本上,在新的稳定版本被删除之前,有一些错误和界面更改需要解决。

关于具有分布式集群的 Python 多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26876898/

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