Python Distributed Process Master

Source: Internet
Author: User

#!/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

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.