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!