Python custom master-slave distributed architecture instance analysis

Source: Internet
Author: User
This article mainly introduces the Python custom master-slave distributed architecture, and analyzes the structure, principles, and specific code implementation skills of the master-slave distributed architecture in the form of instances, for more information about the Python custom master-slave distributed architecture, see the next article. This article analyzes the structure, principles, and specific code implementation skills of the master-slave distributed architecture in the form of instances, for more information, see

This example describes the Python custom master-slave distributed architecture. We will share this with you for your reference. The details are as follows:

Environment:Win7 x64, Python 2.7, and apsched1_2.1.2.

The schematic diagram is as follows:

Code section:

(1) central node:

# Encoding = UTF-8 # author: walker # date: 2014-12-03 # function: Center node (mainly used to assign tasks) import SocketServer, socket, QueueCenterIP = '2017. 0.0.1 '# center node IPCenterListenPort = 9999 # center node listening port CenterClient = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) # socketTaskQueue = Queue used by the central node to send network messages. queue () # task Queue # obtain the Task Queue def GetTaskQueue (): for I in range (1, 11): TaskQueue. put (str (I) # The callback function of CenterServer. when a udp packet is received, class MyUDPHan is triggered. Dler (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 () # obtain the 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, SocketServerfrom apscheduler. scheduler import SchedulerCenterIP = '2017. 0.0.1 '# central node IPCenterListenPort = 9999 # listening port of the central node NodeIP = socket. gethostbyname (socket. gethostname () # task node IPNodeClient = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) # socket used by the task node to send network messages # task: Send Network information def jobSendNetMsg (): msg = ''if No DeServer. 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 the scheduled task def InitTimer (): sched = schedmer () sched. add_interval_job (jobSendNetMsg, seconds = 1) sched. start () # run the task def ExecTask (task): print ('exectask' + task + '... ') Time. sleep (2) print ('exectask' + task + 'over') # The callback function of NodeServer. When udp packets are received, the 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 ()


For more articles on Python user-defined master-slave distributed architecture instance analysis, refer to PHP Chinese network!

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.