Skip to content

Commit e144e27

Browse files
committed
Use aiohttp.ClientSession in crawler.
Beginning with aiohttp 0.16, ClientSession is preferred to using TCPConnector directly, for connection pooling HTTP keep-alive.
1 parent 3228daf commit e144e27

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

crawler/code/crawling.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self, roots,
6161
self.q = Queue(loop=self.loop)
6262
self.seen_urls = set()
6363
self.done = []
64-
self.connector = aiohttp.TCPConnector(loop=self.loop)
64+
self.session = aiohttp.ClientSession(loop=self.loop)
6565
self.root_domains = set()
6666
for root in roots:
6767
parts = urllib.parse.urlparse(root)
@@ -83,7 +83,7 @@ def __init__(self, roots,
8383

8484
def close(self):
8585
"""Close resources."""
86-
self.connector.close()
86+
self.session.close()
8787

8888
def host_okay(self, host):
8989
"""Check if a host should be crawled.
@@ -172,11 +172,9 @@ def fetch(self, url, max_redirect):
172172
exception = None
173173
while tries < self.max_tries:
174174
try:
175-
response = yield from aiohttp.request(
176-
'get', url,
177-
connector=self.connector,
178-
allow_redirects=False,
179-
loop=self.loop)
175+
response = yield from self.session.get(
176+
url, allow_redirects=False)
177+
180178
if tries > 1:
181179
LOGGER.info('try %r for %r success', tries, url)
182180

@@ -229,6 +227,8 @@ def fetch(self, url, max_redirect):
229227
self.q.put_nowait((link, self.max_redirect))
230228
self.seen_urls.update(links)
231229

230+
yield from response.release()
231+
232232
@asyncio.coroutine
233233
def work(self):
234234
"""Process queue items forever."""

crawler/code/requirements-py33.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
# Then install these packages with "pip install -r requirements-py33.txt".
88

99
asyncio
10-
aiohttp
10+
aiohttp>=0.16

crawler/code/requirements-py34.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
#
77
# Then install this package with "pip install -r requirements-py34.txt".
88

9-
aiohttp
9+
aiohttp>=0.16

crawler/crawler.markdown

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ We entice you with a promise. It is possible to write asynchronous code that com
298298
```python
299299
@asyncio.coroutine
300300
def fetch(self, url):
301-
response = yield from aiohttp.request('get', url)
301+
response = yield from self.session.get(url)
302302
body = yield from response.read()
303303
```
304304

@@ -806,6 +806,10 @@ class Crawler:
806806
self.q = Queue()
807807
self.seen_urls = set()
808808

809+
# aiohttp's ClientSession does connection pooling and
810+
# HTTP keep-alives for us.
811+
self.session = aiohttp.ClientSession(loop=self.loop)
812+
809813
# Put (URL, max_redirect) in the queue.
810814
self.q.put((root_url, self.max_redirect))
811815
```
@@ -935,8 +939,8 @@ The crawler fetches "foo" and sees it redirects to "baz", so it adds "baz" to th
935939
@asyncio.coroutine
936940
def fetch(self, url, max_redirect):
937941
# Handle redirects ourselves.
938-
response = yield from aiohttp.request(
939-
'get', url, allow_redirects=False)
942+
response = yield from self.session.get(
943+
url, allow_redirects=False)
940944

941945
if is_redirect(response):
942946
if max_redirect > 0:

0 commit comments

Comments
 (0)