首页 > 代码库 > Python 分布式进程Master
Python 分布式进程Master
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import random, time, queue
from multiprocessing.managers import BaseManager
from multiprocessing import freeze_support
‘‘‘
master进程:调度进程,作用分配任务和接收结果
这部分内容与官网教程,有些出入
‘‘‘
# 发送任务的对列
queue_t = queue.Queue()
# 接收结果的对列
queue_rs = queue.Queue()
# 从BaseManager继承QueueManager
class QueueManager(BaseManager):
pass
def return_queue_t():
global queue_t
return queue_t
def return_queue_r():
global queue_rs
return queue_rs
def master():
# 把两个Queue都注册到网络上, callable参数关联了Queue对象:
QueueManager.register("get_queue_t", callable=return_queue_t)
QueueManager.register("get_queue_rs", callable=return_queue_r)
# 绑定端口5000, 设置验证码love8013
manager = QueueManager(address=("127.0.0.1", 5000), authkey=b"love8013")
# 启动Queue
manager.start()
# 获得通过网络访问的Queue对象
t = manager.get_queue_t()
rs = manager.get_queue_rs()
# 放几个任务进去,计算每个数的4次方
for i in range(10):
n = random.randint(0, 10000)
print("Put %d into the task queue." % n)
t.put(n) # 把每个数放进任务队列中,放在队列中的是int
# 从result队列读取结果
print("Try to get results.")
for i in range(10):
r = rs.get(timeout=10)
print("Result: %s" % r)
# 关闭:
manager.shutdown()
print(‘master exit.‘)
if __name__ == "__main__":
freeze_support()
master()
Python 分布式进程Master