FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-2/gevent_test.py at master · ossoen/python-2 · GitHub
ossoen
/
python-2
Public
forked from
zhanghe06/python
Notifications
You must be signed in to change notification settings
Fork
1
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-2
/
gevent_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (41 loc) · 1.39 KB
Breadcrumbs
python-2
/
gevent_test.py
Copy path
File metadata and controls
52 lines (41 loc) · 1.39 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
46
47
48
49
50
51
52
# coding:utf-8
__author__
=
'zhanghe'
import
gevent
import
random
import
time
# from gevent import monkey
# monkey.patch_all()
def
task
(
pid
):
gevent
.
sleep
(
random
.
randint
(
0
,
2
)
*
0.001
)
# 模拟真实环境,等待时间随机,同步:0.183,顺序执行;异步:0.012,随机执行。
# gevent.sleep(0.1) # 如果等待时间固定,同步:10秒,顺序执行;异步:0.1秒多,表现为顺序执行。
print
(
'Task'
,
pid
,
'done'
)
def
synchronous
():
# 同步
# for i in range(1,100):
for
i
in
xrange
(
100
):
task
(
i
)
def
asynchronous
():
# 异步
threads
=
[
gevent
.
spawn
(
task
,
i
)
for
i
in
xrange
(
100
)]
gevent
.
joinall
(
threads
)
print
(
'Synchronous:'
)
start
=
time
.
time
()
synchronous
()
stop
=
time
.
time
()
print
stop
-
start
print
(
'Asynchronous:'
)
start
=
time
.
time
()
asynchronous
()
stop
=
time
.
time
()
print
stop
-
start
# 测试结果:
# gevent.sleep(0.1) 第一次
# 猴子补丁,同步:10.141;异步0.111
# 没有补丁,同步:10.148;异步0.126
# gevent.sleep(0.1) 第二次
# 猴子补丁,同步:10.241;异步0.112
# 没有补丁,同步:10.130;异步0.113
# gevent.sleep(random.randint(0,2)*0.001) 第一次
# 猴子补丁,同步:0.214;异步0.013
# 没有补丁,同步:0.182;异步0.015
# gevent.sleep(random.randint(0,2)*0.001) 第二次
# 猴子补丁,同步:0.176;异步0.016
# 没有补丁,同步:0.183;异步0.011
Back
|
FazBrowse Home
|
New Git URL