python中的BaseManager通信(二)文件二分

提供服务部分(运行时在接收端未打开前不能关闭)

 1 #mainsec.py
 2 from multiprocessing import Process, Queue
 3 from multiprocessing.managers import BaseManager
 4 class Worker(Process):
 5     def __init__(self, q):
 6         self.q = q
 7         super(Worker, self).__init__()
 8     def run(self):
 9         self.q.put('local hello')
10 if __name__ == '__main__':
11     queue = Queue()
12     w = Worker(queue)
13     w.start()
14     class QueueManager(BaseManager): pass
15 
16     QueueManager.register('get_queue', callable=lambda: queue)
17     m = QueueManager(address=('127.0.0.1', 50000), authkey='abc')
18     s = m.get_server()
19     s.serve_forever()

接收方(接收):

1 #subthir.py
2 from multiprocessing.managers import BaseManager
3 class QueueManager(BaseManager): pass
4 QueueManager.register('get_queue')
5 m = QueueManager(address=('127.0.0.1', 50000), authkey='abc')
6 m.connect()
7 queue = m.get_queue()
8 print m.address
9 print queue.get()

结果:

 1 ('127.0.0.1', 50000)

 2 local hello 

 

posted @ 2015-07-02 00:34  何似王  阅读(894)  评论(0编辑  收藏  举报