Python自訂主從分布式架構執行個體分析,python執行個體分析

來源:互聯網
上載者:User

Python自訂主從分布式架構執行個體分析,python執行個體分析

本文執行個體講述了Python自訂主從分布式架構。分享給大家供大家參考,具體如下:

環境:Win7 x64,Python 2.7,APScheduler 2.1.2。

原理圖如下:

代碼部分:

(1)、中心節點:

#encoding=utf-8#author: walker#date: 2014-12-03#function: 中心節點(主要功能是分配任務)import SocketServer, socket, QueueCenterIP = '127.0.0.1'  #中心節點IPCenterListenPort = 9999  #中心節點監聽連接埠CenterClient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #中心節點用於發送網路訊息的socketTaskQueue = Queue.Queue() #任務隊列#擷取任務隊列def GetTaskQueue():  for i in range(1, 11):    TaskQueue.put(str(i))#CenterServer的回呼函數,在接受到udp報文是觸發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() #擷取任務隊列CenterServer = SocketServer.UDPServer((CenterIP, CenterListenPort), MyUDPHandler)print('Listen port ' + str(CenterListenPort) + ' ...')CenterServer.serve_forever()

(2)、任務節點:

#encoding=utf-8#author: walker#date: 2014-12-03#function: 任務節點(請求/接收/執行任務)import time, socket, SocketServerfrom apscheduler.scheduler import SchedulerCenterIP = '127.0.0.1'  #中心節點IPCenterListenPort = 9999  #中心節點監聽連接埠NodeIP = socket.gethostbyname(socket.gethostname())  #任務節點自身IPNodeClient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  #任務節點用於發送網路訊息的socket#任務:發送網路資訊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))#添加並啟動定時任務def InitTimer():  sched = Scheduler()  sched.add_interval_job(jobSendNetMsg, seconds=1)  sched.start()#執行任務def ExecTask(task):  print('ExecTask ' + task + ' ...')  time.sleep(2)  print('ExecTask ' + task + ' over')#NodeServer的回呼函數,在接受到udp報文是觸發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()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.