#!/usr/bin/env pythonpython
# -*- coding:utf-8 -*-網絡
# author: Changhua Gongdom
import random, time, queueide
from multiprocessing.managers import BaseManager對象
from multiprocessing import freeze_support繼承
'''教程
master進程:調度進程,做用分配任務和接收結果隊列
這部份內容與官網教程,有些出入進程
'''ip
# 發送任務的對列
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()