Python Distributed Process Worker

Source: Internet
Author: User

#!/usr/bin/env python

#-*-Coding:utf-8-*-

# Author:changhua Gong


Import random, time, queue

From multiprocessing.managers import Basemanager

From queue import queue


‘‘‘

Worker processes: Performing tasks, feedback results

This part of the content and the official website tutorial, some discrepancy

‘‘‘



# Inherit from Basemanager QueueManager

Class QueueManager (Basemanager):

Pass


# get the corresponding queue from the network

Queuemanager.register ("get_queue_t")

Queuemanager.register ("Get_queue_rs")

# Connect to the server, which is the machine running task_master.py:

SERVER_IP = "127.0.0.1"

Print ("Connect to server...%s"% server_ip)

Manager = QueueManager (server_ip, address=, authkey=b "love8013") # Ensure that the port and key are consistent

# from the network connection

Manager.connect ()

# Get a Queue object accessed over the network

t = manager.get_queue_t ()

rs = Manager.get_queue_rs ()

Print ("1")

# read a task from the task queue and write results to the result queue

For I in range (10):

Try

n = t.get (timeout=1)

Print (' Run task%d *%d ... '% (n, N))

R = '%d *%d =%d '% (n, N, n*n)

Time.sleep (1)

Rs.put (R)

Except Queue.empty:

Print (' Task queue is empty. ')

Print (' Worker exit. ')


Python Distributed Process Worker

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.