• python multiprocessing example


    python multiprocessing example


    Server Code:


    #!/usr/bin/python  
    #-*- coding: UTF-8 -*-
    # mpserver.py
    #
    # Queues are thread and process safe.
    
    from multiprocessing.managers import BaseManager
    
    # g as a server process state
    g = 10000
    
    class MathClass(object):
        def add(self, x, y):
            return x + y + g
        def mul(self, x, y):
            return x * y
    
    class MathManager(BaseManager):
        pass
    
    MathManager.register('Math', MathClass)
    
    
    manager = MathManager(address=('', 50000), authkey='abc')
    server = manager.get_server()
    server.serve_forever()
    

    Client Code:

    #!/usr/bin/python  
    #-*- coding: UTF-8 -*-
    # mpclient.py
    #
    # Queues are thread and process safe.
    
    from multiprocessing.managers import BaseManager
    
    class MathClass(object): pass    
    
    class MathManager(BaseManager): pass
    
    MathManager.register('Math', MathClass)
    
    
    manager = MathManager(address=('', 50000), authkey='abc')
    manager.connect()
    m = manager.Math()
    
    print m.add(100, 20)


  • 相关阅读:
    o9.17,习题
    09.17,二维数组,地图
    09.15,一维数组,冒泡排序
    09.11 小兔 成兔问题
    09.01,学习习题
    FTPHelper
    Wpf发送接收 win32消息
    win32Helper
    xml 封装类
    C# 多进程安全
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7244214.html
Copyright © 2020-2023  润新知