Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
aabbccgithub
Python24
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python24
/
03Thread
/
day04
/
basic02.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
56 lines (39 loc) · 1.79 KB
master
Breadcrumbs
Python24
/
03Thread
/
day04
/
basic02.py
Copy path
Top
File metadata and controls
Code
Blame
56 lines (39 loc) · 1.79 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
53
54
55
56
"""
多任务下载器,使用协程,本质就是使用**当前进程**在**不同**的函数代码内切换执行
协程会在 urllib.request.urlopen(url_str)这行阻塞的代码中利用破解进行自动切换
1- 进程切换需要的资源大,效率低
2- 县城需要的资源一般,效率一般
3- 协程切换需要的资源很少,效率高
4- 进程:一条流水线
5- 线程: 一条流水线中的不同工人 --> 单进程,多线程
6- 一条流水线并不是线程越多越好: 多个流水线多个工人 --> 多进程,多线程
7- 为再次提高效率,某条流水线的某个员工**临时没事,或者在等待的状态**,那么他就会去帮其他的人做事
充分利用时间 --> 协程方式(微线程)
总之: 协程就是尽可能的压榨硬件性能
"""
# 引入monkey进行阻塞破解
from
gevent
import
monkey
monkey
.
patch_all
()
import
gevent
# 引入urllib,进行网页响应,下载网页的html代码
import
urllib
.
request
# time用来及时
import
time
def
down_html
(
url_str
):
# 向远程服务器发起请求 -> 一个结果对象
print
(
"开始请求:%s"
%
url_str
)
response
=
urllib
.
request
.
urlopen
(
url_str
)
# io获取data
data
=
response
.
read
()
print
(
"获取网页数据成功 %s 共计 %d 字节"
%
(
url_str
,
len
(
data
)))
def
main
():
start_time
=
time
.
time
()
# 不破解的话就会一个一个read网页,因为gevent默认是不会手动切换协程的
g1
=
gevent
.
spawn
(
down_html
,
"http://qq.com"
)
g2
=
gevent
.
spawn
(
down_html
,
"http://google.com"
)
g3
=
gevent
.
spawn
(
down_html
,
"http://twitter.com"
)
gevent
.
joinall
([
g1
,
g2
,
g3
])
end_time
=
time
.
time
()
print
(
"总共耗时: %s"
%
(
end_time
-
start_time
))
if
__name__
==
'__main__'
:
main
()
You can’t perform that action at this time.