task_master.txt
task_worker.txt
t@localhost untitled$ python3 task_master.py
Put task 6811
Put task 5164
Put task 8492
Put task 177
Put task 5496
Put task 8724
Put task 6422
Put task 2887
Put task 287
Put task 876
Result : 6811 * 6811 = 46389721
Result : 5164 * 5164 = 26666896
Result : 8492 * 8492 = 72114064
Result : 177 * 177 = 31329
Result : 5496 * 5496 = 30206016
Result : 8724 * 8724 = 76108176
Result : 6422 * 6422 = 41242084
Result : 2887 * 2887 = 8334769
Result : 287 * 287 = 82369
Result : 876 * 876 = 767376
master exit.
t@localhost untitled$
参考学习:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431929340191970154d52b9d484b88a7b343708fcc60000#0
task_worker.txt
点击(此处)折叠或打开
- #!/usr/bin/env python3
- #-*- coding:utf-8 -*-
- '''
- '''
- import time,random,queue
- from multiprocessing.managers import BaseManager
-
- task_queue = queue.Queue()
- result_queue = queue.Queue()
-
- class QueueManager(BaseManager):
- pass
-
- QueueManager.register('get_task_queue', callable=lambda: task_queue)
- QueueManager.register('get_result_queue', callable=lambda: result_queue)
-
- manager = QueueManager(address=('', 5000), authkey=b'talen')
- manager.start()
- task = manager.get_task_queue()
- result = manager.get_result_queue()
- for i in range(10):
- n = random.randint(0,9000)
- print('Put task %d' % n)
- task.put(n)
- for i in range(10):
- r=result.get(timeout=10)
- print("Result : %s " % r)
- manager.shutdown()
-
- print('master exit.')
点击(此处)折叠或打开
- #!/usr/bin/env python3
- #-*- coding:utf-8 -*-
- '''
- '''
-
-
- import time, queue, sys
- from multiprocessing.managers import BaseManager
- class QueueManger(BaseManager):
- pass
- QueueManger.register('get_task_queue')
- QueueManger.register('get_result_queue')
- server_addr='127.0.0.1'
- print('Connect to server %s '% server_addr)
- m=QueueManger(address=(server_addr,5000), authkey=b'talen')
- m.connect()
- task=m.get_task_queue()
- result=m.get_result_queue()
- for i in range(10):
- try:
- n=task.get(timeout=1)
- print('run task %d * %d ...' %(n,n))
- r='%d * %d = %d' % (n,n,n*n)
- time.sleep(1)
- result.put(r)
- except Queue.Empty:
- print('task queue is empty')
- print('worker exit.')
Put task 6811
Put task 5164
Put task 8492
Put task 177
Put task 5496
Put task 8724
Put task 6422
Put task 2887
Put task 287
Put task 876
Result : 6811 * 6811 = 46389721
Result : 5164 * 5164 = 26666896
Result : 8492 * 8492 = 72114064
Result : 177 * 177 = 31329
Result : 5496 * 5496 = 30206016
Result : 8724 * 8724 = 76108176
Result : 6422 * 6422 = 41242084
Result : 2887 * 2887 = 8334769
Result : 287 * 287 = 82369
Result : 876 * 876 = 767376
master exit.
t@localhost untitled$
t@localhost untitled$ python3 task_worker.py
Connect to server 127.0.0.1
run task 6811 * 6811 ...
run task 5164 * 5164 ...
run task 8492 * 8492 ...
run task 177 * 177 ...
run task 5496 * 5496 ...
run task 8724 * 8724 ...
run task 6422 * 6422 ...
run task 2887 * 2887 ...
run task 287 * 287 ...
run task 876 * 876 ...
worker exit.
t@localhost untitled$
Connect to server 127.0.0.1
run task 6811 * 6811 ...
run task 5164 * 5164 ...
run task 8492 * 8492 ...
run task 177 * 177 ...
run task 5496 * 5496 ...
run task 8724 * 8724 ...
run task 6422 * 6422 ...
run task 2887 * 2887 ...
run task 287 * 287 ...
run task 876 * 876 ...
worker exit.
t@localhost untitled$
参考学习:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431929340191970154d52b9d484b88a7b343708fcc60000#0