Skip to content

Commit 32a1b8c

Browse files
author
cztps2
committed
xx
1 parent 19867c7 commit 32a1b8c

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

11. gevent_spider.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import gevent.monkey
2+
3+
gevent.monkey.patch_all()
4+
5+
import gevent
6+
import blog_spider
7+
import time
8+
9+
begin = time.time()
10+
for url in blog_spider.urls:
11+
blog_spider.craw(url)
12+
end = time.time()
13+
print("single thread, cost = ", end - begin)
14+
15+
begin = time.time()
16+
tasks = [gevent.spawn(blog_spider.craw, url) for url in blog_spider.urls]
17+
gevent.joinall(tasks)
18+
end = time.time()
19+
print("gevent, cost = ", end - begin)

Python并发编程简介.pptx

17.5 KB
Binary file not shown.

blog_spider.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
def craw(url):
11+
print("craw url: ", url)
1112
r = requests.get(url)
1213
return r.text
1314

0 commit comments

Comments
 (0)