python - 用Python构建分布式计算网络

标签 python python-2.7 sockets

所以我有大量的数据需要处理,为了做到这一点,我使用了我能得到的一切,我 parent 的电脑,我女朋友的电脑,我的电脑,我兄弟的电脑。

他们同意借给我一些处理能力,并且处理程序只使用他们计算机 4 个内核中的 1 个。 我将设置一些东西,在计算机启动时启动从属设备

我自己编写了这个“分布式计算程序”,我刚刚通过谷歌了解了套接字,我想确保我没有犯一个大错误

据我了解,套接字只是一种方式,A只能向B发送数据,如果B需要向A发送数据,则需要打开其他端口上的另一个套接字。

the "distributor" is the program that orchestrates the computing, it sends data to crunch to all the slaves, it is running on a cheap dedicated server
the "slaves" ask data from the distributor and compute stuff, store the result, then ask for more data to crunch

the "distributor" has a registration_port_distributor : 15555
the "slaves" have a registration_port_slave : 14444 (yes the same for each slaves)
work_port = registration_port_distributor + 1

the distributer boots
start of the loop
    wait for a slave connection
    a slave connect to port 15555 (registration_port_distributor) and tell the distributor "I am 'slave_name', give me 2 ports to work on my port 14444 (registration_port_slave)"
    the disbtributor connect to the slave on port 'registration_port_slave' and give it "work_port" (data_reception_port) for receiving data and work_port+1 (data_request_port) so that the slave can request new data to crunch
    work_port is incremented by 2

从此时起,从设备可以从“data_reception_port”上的连接接收要处理的数据 它可以请求从“data_request_port”上的连接处理新数据

我在这里看到的唯一问题是如果 2 个从站尝试同时连接,但这很容易 修复了在每个从设备上使用 while 循环并休眠 5 秒以重新尝试连接的问题。

你觉得怎么样?

谢谢。

ps:是的,从站不会发回结果,我将手动获取它们,或稍后实现。

pps:稍后会上传到我的github,现在代码很乱,我正在测试各种东西。

最佳答案

From what I understand a socket is one way only, A can only send data to B, if B needs to send data to A then an other socket on an other port need to be opened.

正如几个人在评论中已经提到的,TCP 套接字是双向的,您可以将其用于双向通信。应用程序的编码方式必须使双方能够相互理解。

from this point a slave can receive data to process from a connection on 'data_reception_port' and it can ask for new data to crunch from a connection on 'data_request_port'

将应用程序模型更改为上述方式后,您将不再需要每侧使用两个单独的端口/连接进行通信。

The only problem I can see here is if 2 slaves try to connect at the same time, but that is easily fixed using a while loop on each slave with a 5 second sleep for reattempting a connection.

请阅读有关 Socket 通信积压的信息。如果传入请求的数量超过了当前可以服务的数量,则请求将被排队(队列中等待的请求的确切数量取决于 backlog 参数)。检查documentation of socket.listen([backlog]) function了解更多信息。

我希望这能解答您的问题。如有任何疑问,请随时进一步询问。

关于python - 用Python构建分布式计算网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48767851/

相关文章:

python - 在 Python 中将单行字符串转换为整数数组

java - 具有内部类和最终变量的线程化 Java 服务器

linux - 按主机名的 IPv6 地址

python - 从类定义中的列表理解访问类变量

python - 如何将图像更改为表示为 NumPy 数组的灰度

android - Qpython 未知 RPC 错误

python - ElementTree find()/findall() 找不到带有命名空间的标签?

php - php:如何与后台运行的PHP脚本进行通信?

Python subprocess.call 似乎忽略参数

python - Django 模型 - SELECT DISTINCT(foo) FROM 表太慢