Python分布式学习(2)

在这段代码我们传递了自己定义的类student!!利用JSON进行序列化.

import random,time,queue,json
from multiprocessing.managers import BaseManager
from multiprocessing import freeze_support

task_queue = queue.Queue()
result_queue = queue.Queue()
def return_task():
    return task_queue
def return_result():
    return result_queue
class QueueManager(BaseManager):
    pass

class student(object):
    def __init__(self,name,grades,age):
        try:
            self.name = str(name)
            self.grades = int(grades)
            self.age = int(age)
        except ValueError:
            print('the argument must be str,int,int')
            raise ValueError

def test():
    QueueManager.register('get_task',callable = return_task)
    QueueManager.register('get_result',callable = return_result)
    manager = QueueManager(address = ('127.0.0.1',5000),authkey = b'abc')#绑定到端口5000,并且设置验证码'abc'
    manager.start()
#获得经过封装之后的task和result
    task,result = manager.get_task(),manager.get_result()
#放一些任务
    name = ['alice','joe','lucy','leo','zoom']
    for i in range(5):
        print('put %d in task'%i)
        std = student(name[i],i*10,i+2)
        t = json.dumps(std, default=lambda obj: obj.__dict__)
        print(t)
        task.put(t)
#获取结果
    print('try to get result')
    for i in range(5):
        r = result.get(timeout = 10)
        r =student(r['name'],r['grades'],r['age'])
        print('get the result :',r)
    manager.shutdown()
    print('master exit')

if __name__ == '__main__':
    freeze_support()
    test()
from multiprocessing.managers import BaseManager
from multiprocessing import freeze_support
import json,math
class QueueManager(BaseManager):
    pass

if __name__ == '__main__':
    QueueManager.register('get_task')
    QueueManager.register('get_result')
    server_adr = '127.0.0.1'
    print('connect to the server%s',server_adr)
    manager = QueueManager(address = (server_adr,5000),authkey = b'abc')
    manager.connect()
    print('connect successfuly')

    task = manager.get_task()
    result = manager.get_result()
    for i in range(5):
        try:
            t = task.get(timeout = 1)
            std = json.loads(t)
            std['grades'] = math.sqrt(std['grades'])*10
            print('now process the task%d'%i)
            result.put(std)
        except Queue.Empty:
            print('the task queue is empty, maybe some task lost?')
    print('wordker exit')

你可能感兴趣的:(Python分布式学习(2))