English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Instance Analysis of Custom Master-Slave Distributed Architecture in Python

This example describes the Python custom master-slave distributed architecture. Shared for everyone's reference, as follows:

Environment:Win7 x64,Python 2。7,APScheduler 2。1。2。

Schematic diagram as follows:

Code section:

(1)、Center Node:

#encoding=utf-8
#author: walker
#date: 2014-12-03
#function: Center node (main function is to distribute tasks)
import SocketServer, socket, Queue
CenterIP = '127.0.0.1'  #IP of the center node
CenterListenPort = 9999  #Listening port of the center node
CenterClient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #Socket used by the center node to send network messages
TaskQueue = Queue.Queue() #Task queue
#Get task queue
def GetTaskQueue():
  for i in range(1, 11):
    TaskQueue.put(str(i))
#CenterServer's callback function, triggered when receiving udp messages
class MyUDPHandler(SocketServer.BaseRequestHandler):
  def handle(self):
    data = self.request[0].strip()
    socket = self.request[1]
    print(data)
    if data.startswith('wait'):
      vec = data.split(':')
      if len(vec) != 3:
        print('Error: len(vec) != 3)
      else:
        nodeIP = vec[1]
        nodeListenPort = vec[2]
        nodeID = nodeIP + : + nodeListenPort
        if not TaskQueue.empty():
          task = TaskQueue.get()
          print('send task ') + task + to ' + nodeID)
          CenterClient.sendto('task:' + task, (nodeIP, int(nodeListenPort))
        else:
          print('TaskQueue is empty!')
GetTaskQueue() #Get task queue
CenterServer = SocketServer.UDPServer((CenterIP, CenterListenPort), MyUDPHandler)
print('Listen port ') + str(CenterListenPort) + ' ...')
CenterServer.serve_forever()

(2)、Task Node:

#encoding=utf-8
#author: walker
#date: 2014-12-03
#function: Task node (request/Receive/Execute task)
import time, socket, SocketServer
from apscheduler.scheduler import Scheduler
CenterIP = '127.0.0.1'  #IP of the center node
CenterListenPort = 9999  #Listening port of the center node
NodeIP = socket.gethostbyname(socket.gethostname())  #IP of the task node
NodeClient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  #Task node socket for sending network messages
#Task: send network information
def jobSendNetMsg():
  msg = ''
  if NodeServer.TaskState == 'wait':
    msg = 'wait:' + NodeIP + : + str(NodeListenPort)
  elif NodeServer.TaskState == 'exec':
    msg = 'exec:' + NodeIP + : + str(NodeListenPort)
  print(msg)
  NodeClient.sendto(msg, (CenterIP, CenterListenPort))
#Add and start timer task
def InitTimer():
  sched = Scheduler()
  sched.add_interval_job(jobSendNetMsg, seconds=)1)
  sched.start()
#Execute task
def ExecTask(task):
  print('ExecTask ') + task + ' ...')
  time.sleep(2)
  print('ExecTask ') + task + ' over')
#Callback function of NodeServer, triggered when receiving a udp packet
class MyUDPHandler(SocketServer.BaseRequestHandler):
  def handle(self):
    data = self.request[0].strip()
    socket = self.request[1]
    print('recv data: ') + data)
    if data.startswith('task'):
      vec = data.split(':')
      if len(vec) != 2:
        print('Error: len(vec) != 2)
      else:
        task = vec[1]
        self.server.TaskState = 'exec'
        ExecTask(task)
        self.server.TaskState = 'wait'
InitTimer()
NodeServer = SocketServer.UDPServer('', 0, MyUDPHandler)
NodeServer.TaskState = 'wait' #(exec/wait)
NodeListenPort = NodeServer.server_address[1]
print('NodeListenPort:' + str(NodeListenPort))
NodeServer.serve_forever()

Readers who are interested in more about Python-related content can check the special topics on this site: 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', 'Classic Tutorial of Python Entry and Advanced', and 'Summary of Python File and Directory Operation Skills'

Hoping the content described in this article will be helpful to everyone in Python program design.

Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report abuse by sending an email to codebox.com (replace # with @) and provide relevant evidence. Once verified, the website will immediately delete the infringing content.

You May Also Like