FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/taskmanager.py at master · eatyorcode/python · GitHub
eatyorcode
/
python
Public
forked from
zhanghe06/python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
taskmanager.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
45 lines (40 loc) · 1.2 KB
Breadcrumbs
python
/
taskmanager.py
Copy path
File metadata and controls
45 lines (40 loc) · 1.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# encoding: utf-8
__author__
=
'zhanghe'
import
random
import
time
import
Queue
from
multiprocessing
.
managers
import
BaseManager
# 发送任务的队列:
task_queue
=
Queue
.
Queue
()
# 接收结果的队列:
result_queue
=
Queue
.
Queue
()
# 从BaseManager继承的QueueManager:
class
QueueManager
(
BaseManager
):
pass
# 把两个Queue都注册到网络上, callable参数关联了Queue对象:
QueueManager
.
register
(
'get_task_queue'
,
callable
=
lambda
:
task_queue
)
QueueManager
.
register
(
'get_result_queue'
,
callable
=
lambda
:
result_queue
)
# 绑定端口5000, 设置验证码'abc':
manager
=
QueueManager
(
address
=
(
''
,
5000
),
authkey
=
'abc'
)
# 启动Queue:
manager
.
start
()
# 获得通过网络访问的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):
while
True
:
try
:
r
=
result
.
get
(
timeout
=
2
)
print
(
'Result: %s'
%
r
)
except
Queue
.
Empty
:
# 2S内取不到结果,返回结果队列为空提示
print
(
'result queue is empty.'
)
# 关闭:
manager
.
shutdown
()
Back
|
FazBrowse Home
|
New Git URL