Python Simple distributed Demo

Source: Internet
Author: User

A server is a master,b server for worker,

Execute taskmanger.py on Server A

#Coding:utf-8ImportRandom,time,queue fromMultiprocessing.managersImportBasemanager#implementing the first step: establishing Task_queue and result_queue for storing tasks and resultsTask_queue=queue.queue () result_queue=Queue.queue ()classQueueManager (basemanager):Pass#Implement the second step: Register the created two queues on the network, using the Register method, the callable parameter is associated with the queue object,#exposing a Queue object to the networkQueuemanager.register ('Get_task_queue', callable=Lambda: Task_queue) Queuemanager.register ('Get_result_queue', callable=Lambda: Result_queue)#Implement the third step: Bind Port 8001, set the authentication password ' Qiye '. This corresponds to the initialization of the objectManager=queuemanager (address= ("', 8001), authkey='LSF')#Implement the fourth step: Start the management, monitor the information channelManager.start ()#Implement step Fifth: Get a Queue object accessed over the network by managing the instance's methodstask=manager.get_task_queue () result=Manager.get_result_queue ()#Implementing step Sixth: adding Tasks forUrlinch["Imageurl_"+STR (i) forIinchRange (10)]:    Print 'put task%s ...'%url task.put (URL)#Get return resultsPrint 'try get result ...' forIinchRange (10):    Print 'result is%s'%result.get (timeout=10)#Close ManagementManager.shutdown ()

Script for worker execution on server B taskworker.py

#Coding:utf-8Import Time fromMultiprocessing.managersImportBasemanager#Create a similar queuemanager:classQueueManager (basemanager):Pass#implementing the first step: Get the method name of the queue using QueueManager registrationQueuemanager.register ('Get_task_queue') Queuemanager.register ('Get_result_queue')#implementing the second step: connecting to the server:SERVER_ADDR ='127.0.0.1'Print('Connect to server%s ...'%server_addr)#the port and authentication password are kept in full alignment with the service process settings:m = QueueManager (address= (SERVER_ADDR, 8001), authkey='LSF')#from the network connection:M.connect ()#implement step three: Get the object for the queue:Task =m.get_task_queue () result=M.get_result_queue ()#Implement step Fourth: Take the task from the task queue and write the results to the result queue: while( notTask.empty ()): Image_url= Task.get (true,timeout=5)        Print('Run task download%s ...'%image_url) Time.sleep (1) Result.put ('%s--->success'%Image_url)#processing Ends:Print('worker exit.')

Python Simple distributed Demo

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.