-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_taskmanager.py
75 lines (62 loc) · 1.89 KB
/
my_taskmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding=utf-8 -*-
# taskmanager.py
import random,time,Queue
from multiprocessing.managers import BaseManager
from multiprocessing import freeze_support
#发送任务的队列
task_queue = Queue.Queue()
#接受结果的队列
result_queue = Queue.Queue()
# 为解决__main__.<lambda> not found问题
def get_task_queue():
return task_queue
# 为解决__main__.<lambda> not found问题
def get_result_queue():
return result_queue
#从BaseManager 继承的QueueManager
class QueueManager(BaseManager):
pass
#把两个Queue都注册到网络上,callable 参数关联了Queue对象
QueueManager.register('get_task_queue', callable=get_task_queue)
# QueueManager.register('get_task_queue',callable=lambda: task_queue)
QueueManager.register('get_result_queue',callable=get_result_queue)
#绑定端口5000,设置验证码 'abc'
manager = QueueManager(address=('127.0.0.1',5000),authkey='abc')
#启动Queue
# manager.start()
# #获取通过网络访问的Queue对象
# task=manager.get_task_queue()
# result=manager.get_result_queue()
# #放几个任务进去
# for i in range(10):
# n = random.random(0,10000)
# print 'Put task %d...' %n
# task.put(n)
# #从result队列读取结果
# print 'Try get result...'
# for i in range(10):
# r = result.get(timeout=10)
# print 'Result: %s' %r
# #关闭
# manager.shutdown()
def communicate():
# 获得通过网络访问的Queue对象:
task = manager.get_task_queue()
result = manager.get_result_queue()
# 放几个任务进去:
for i in range(10):
n = random.randint(0, 10000)
print('Put task %d...' % n)
task.put(n)
# 从result队列读取结果:
print('Try get results...')
for i in range(10):
r = result.get(timeout=10)
print('Result: %s' % r)
# 关闭:
manager.shutdown()
if __name__ == '__main__':
freeze_support()
# 启动Queue:
manager.start()
communicate()