#!/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 process: Dispatch process, Role Assignment task and receive result
This part of the content and the official website tutorial, some discrepancy
‘‘‘
# Send a task to a column
queue_t = queue. Queue ()
# The pairs of columns that receive the results
Queue_rs = queue. Queue ()
# Inherit from 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 ():
# Two queues are registered on the network, the callable parameter is associated with the queue object:
Queuemanager.register ("get_queue_t", callable=return_queue_t)
Queuemanager.register ("Get_queue_rs", Callable=return_queue_r)
# Bind Port 5000, set verification code love8013
Manager = QueueManager (address= ("127.0.0.1", Authkey=b "love8013")
# Start Queue
Manager.start ()
# Get a Queue object accessed over the network
t = manager.get_queue_t ()
rs = Manager.get_queue_rs ()
# Put a few quests in, and count the 4 times of each number
For I in range (10):
n = random.randint (0, 10000)
Print ("Put%d into the task queue."% n)
T.put (n) # put each number in the task queue, and the int in the queue
# read results from the result queue
Print ("Try to get results.")
For I in range (10):
R = Rs.get (timeout=10)
Print ("Result:%s"% r)
Close
Manager.shutdown ()
Print (' Master exit. ')
if __name__ = = "__main__":
Freeze_support ()
Master ()
Python Distributed Process Master