欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

高性能分布式计算(HPC)作业2——节点通信,发布计算任务

程序员文章站 2022-07-12 17:13:27
...

1️⃣ 作业要求

  • 建立两节点间的通信;
  • 从节点 1 向节点 2 发送一个python语言编写的源程序 A,节点 2 执行程序 A,并向节
    点 1 返回计算结果;
  • 程序 A 可在节点 2 上独立完成运行,无需其它条件支持。

2️⃣程序

2️⃣.1️⃣ 程序1——实现通信

import socket
import time
import threading
import os
import sys


def ReceiveFile(conn):
    while True:  # 连接成功后一直使用当前连接,直到退出
        with open("recv.py", "ab") as f:
            data = conn.recv(1024)
            if data == b'quit':
                break
            if data != b'success':
                f.write(data)
            conn.send("success".encode())
    print("文件test.py已经接收!存储为recv.py")
    f.close()


def SendAnswer(conn):
    while True:
        if os.path.exists("recv.py"):
            ans = os.popen("python recv.py")
            ansRead = ans.read()
            print("recv.py运行完毕,得到结果为%s" % (str(ansRead)))
            with open('output.txt', "w") as f:
                f.write(ansRead)
                f.close()
            print("将得到的结果写入output.txt")
            with open('output.txt', 'rb') as f:
                for i in f:
                    conn.send(i)
                    data = conn.recv(1024)
                    if data != b'success':
                        break
                conn.send('quit'.encode())
            print("将output.txt发送完毕!")
            break
    conn.close()


def SendPyFile(conn):
    with open('test.py', 'rb') as f:
        for i in f:
            conn.send(i)
            data = conn.recv(1024)
            if data != b'success':
                break
    print("文件test.py已经发送!")
    conn.send('quit'.encode())


def ReceiveAnswer(conn):
    while True:
        with open("recv_output.txt", "ab") as f:
            data = conn.recv(1024)
            if data == b'quit':
                break
            if data != b'success':
                f.write(data)
            conn.send("success".encode())
    print("结果接收完毕,存储在recv_output.txt!")
    f.close()
    conn.send('quit'.encode())


class msgThread(threading.Thread):
    def __init__(self, conn, flag):
        threading.Thread.__init__(self)
        self.conn = conn
        self.flag = flag

    def run(self):
        if self.flag == "send_file":
            SendPyFile(self.conn)
        elif self.flag == "receive_answer":
            ReceiveAnswer(self.conn)
        elif self.flag == "receive_file":
            ReceiveFile(self.conn)
        else:
            SendAnswer(self.conn)

# 改写线程类
def Client(address):
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while True:
        try:
            client.connect((address, 6999))  # 建立一个链接,连接到本地的6999端口
            break
        except:
            print("等待侦听!")
            time.sleep(1)
    Thread_receive = msgThread(client, "receive_file")
    Thread_send = msgThread(client, "send_answer")
    Thread_receive.start()
    Thread_send.start()


def Server():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('127.0.0.1', 6999))  # 绑定要监听的端口
    server.listen(5)  # 开始监听 表示可以使用五个链接排队
    conn, addr = server.accept()  # 等待链接,多个链接的时候就会出现问题,其实返回了两个值
    print("侦听器已启动!port:6999")
    print("连接建立,地址在%s" % (str(addr)))
    Thread_receive = msgThread(conn, "send_file")
    Thread_send = msgThread(conn, "receive_answer")
    Thread_receive.start()
    Thread_send.start()


if __name__ == "__main__":
    BootMode = input("请选择启动方式(1(控制节点)或2(计算节点)):\n")
    if BootMode == '1':
        Server()
    else:
        port = input("请输入侦听服务器地址(默认127.0.0.1):\n")
        Client(port)

2️⃣.2️⃣ 程序2——计算任务函数

import random


a = []

for i in range(10):
    a.append(random.random() * 10)
    
print(max(a),min(a))
相关标签: 分布式计算