pythonでのBaseManager通信(二)ファイル二分

4862 ワード

サービス・セクションの提供(実行時に受信側が開かないまで閉じることはできません)
 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