码迷,mamicode.com
首页 > 编程语言 > 详细

python multiprocessing example

时间:2017-07-27 13:36:22      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:class   connect   pass   get   forever   enter   reg   address   sel   

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)


python multiprocessing example

标签:class   connect   pass   get   forever   enter   reg   address   sel   

原文地址:http://www.cnblogs.com/slgkaifa/p/7244214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!