Python custom master-Slave Distributed architecture example analysis _python

Source: Internet
Author: User

The example in this article describes the Python custom master-slave distributed architecture. Share to everyone for your reference, specific as follows:

Environment: Win7 X64,python 2.7,apscheduler 2.1.2.

The schematic diagram is as follows:

Code section:

(1), center node:

#encoding =utf-8 #author: Walker #date: 2014-12-03 #function: central node (main function is to assign tasks) import Socketserver, socket, Queue Centerip = ' 127.0.0.1 ' #中心节点IP centerlistenport = 9999 #中心节点监听端口 centerclient = Socket.socket (socket.af_inet, socket.
    SOCK_DGRAM) #中心节点用于发送网络消息的socket taskqueue = Queue.queue () #任务队列 #获取任务队列 def gettaskqueue (): For I in range (1, 11): Taskqueue.put (str (i)) #CenterServer的回调函数, in the received UDP message is triggered 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), Task node:

#encoding =utf-8 #author: Walker #date: 2014-12-03 #function: Task node (request/Receive/Perform task) import time, socket, socketserver from APSC Heduler.scheduler Import Scheduler Centerip = ' 127.0.0.1 ' #中心节点IP centerlistenport = 9999 #中心节点监听端口 Nodeip = Socket.geth Ostbyname (Socket.gethostname ()) #任务节点自身IP nodeclient = Socket.socket (socket.af_inet, socket. SOCK_DGRAM) #任务节点用于发送网络消息的socket #任务: Send network Information def jobsendnetmsg (): msg = ' If nodeserver.taskstate = ' Wait ': Msg = ' Wait: ' + Nodeip + ': ' + str (nodelistenport) elif nodeserver.taskstate = = ' EXEC ': msg = ' EXEC: ' + Nodeip + ': ' + S  TR (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 (' Exect Ask ' + Task + ' ... ') time.sleep (2) print (' Exectask ' + task + ' over ') #NodeServer的回调函数, the received UDP message is the triggering class Myudphand Ler (Socketserver.baserequesthandler): def handle (SELF): data = Self.request[0].strip () socket = self.request[1] Print (' recv data: ' + data ') if Data.startswit H (' Task '): VEC = Data.split (': ') If Len (VEC)!= 2:print (' Error:len (VEC)!= 2 ') else:t Ask = vec[1] self.server.TaskState = ' EXEC ' exectask (Task) self.server.TaskState = ' Wait ' inittime R () Nodeserver = Socketserver.udpserver ((', 0), myudphandler) nodeserver.taskstate = ' Wait ' # (exec/wait) Nodelistenport

 = nodeserver.server_address[1] Print (' nodelistenport: ' + str (nodelistenport)) Nodeserver.serve_forever ()

More information about Python-related content can be viewed in this site: "Python URL Operation tips Summary", "Python Picture Operation tips Summary", "Python data structure and algorithm tutorial", "Python Socket Programming Skills Summary", " Python function Usage Tips Summary, python string manipulation tips, Python introductory and Advanced classic tutorials, and Python file and directory how-to tips

I hope this article will help you with Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.