Python Distributed Process experience

Source: Internet
Author: User

Took a little time to experience a python distributed process, a bit like the meaning of distributed computing, but I do not have this need, the first simple experience of the script sent, for the passing of the master advice

Note: You need to download the multiprocessing Python package support first.

Management side:

Cat task_manager.py

#!/usr/bin/env python

#coding: UTF8




Import Random,time,queue,json

From multiprocessing.managers import Basemanager




#发送任务的队列:


Task_queue = Queue.queue ()


#接收结果的队列:


Result_queue = Queue.queue ()


#从BaseManager inherited QueueManager:

Class QueueManager (Basemanager):

Pass



#把两个Queue are all on the network, the callable parameter is associated with the queue object:

Queuemanager.register (' Get_task_queue ', callable=lambda:task_queue)

Queuemanager.register (' Get_result_queue ', callable=lambda:result_queue)


#绑定端口5000 Setting the Captcha ' 123456 '


Manager = QueueManager (address= (","), authkey= ' 123456 ')


#启动Queue:

Manager.start ()


#获得通过网络访问的Queue对象:

task = Manager.get_task_queue ()

result = Manager.get_result_queue ()


#放任务进去:

print ' Put tasks ... '

Shell_cmd = [' Date ', ' hostname ', ' uptime ']

print ' Put task \033[1;31;2m [%s] \033[0m: '%shell_cmd


While True:

print ' Get result ... '

Task.put (Shell_cmd)

R = Result.get ()

print ' result:\033[1;32;40m%s \033[0m ... '%r


Manager.shutdown ()


Client:


#!/usr/bin/env python

#coding: UTF8


Import Time,sys,queue,os,commands

From multiprocessing.managers import Basemanager


#创建类似的QueueManager:

Class QueueManager (Basemanager):

Pass



#由于这个queuemanager only get the queue from the network, so only the name is provided when registering:

Queuemanager.register (' Get_task_queue ')

Queuemanager.register (' Get_result_queue ')


#连接到服务器 is the Zhun.

server_addr = ' 10.1.10.15 '

print ' Connect to server%s ... '%server_addr


#端口和验证码注意保持与taskmanager. PY-Consistent

m = QueueManager (address= (server_addr,5000), authkey= ' 123456 ')

#从网络连接

M.connect ()

#获取Queue的对象

task = M.get_task_queue ()

result = M.get_result_queue ()


#从task队列取作务 and write the results in the result queue


n = task.get (timeout=1)

Print N,type (n)


S,v =commands.getstatusoutput ("ifconfig | grep ' inet addr: ' | Grep-v ' 127.0.0.1 ' | Cut-d:-f2 | awk ' {print '} ')

R_dic = {v:[]}

For I in N:


print ' Run task \033[1;36;40m%s \033[0m '%i

R = Os.popen (i). read (). split (' \ n ') [:-1]

R_dic[v].append (R)

Time.sleep (1)

Result.put (R_dic)

Print R_dic

#处理结束:

print ' worker exit. '


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.