10000 Add pool-closing to fetch3 example. · Python-Repository-Hub/asyncio@53e4794 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53e4794

Browse files
committed
Add pool-closing to fetch3 example.
1 parent 6ddd94b commit 53e4794

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

examples/fetch3.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def __init__(self, verbose=False):
1818
self.verbose = verbose
1919
self.connections = {} # {(host, port, ssl): (reader, writer)}
2020

21+
def close(self):
22+
for _, writer in self.connections.values():
23+
writer.close()
24+
2125
@coroutine
2226
def open_connection(self, host, port, ssl):
2327
port = port or (443 if ssl else 80)
@@ -187,18 +191,21 @@ def read(self):
187191
@coroutine
188192
def fetch(url, verbose=True, max_redirect=10):
189193
pool = ConnectionPool(verbose)
190-
for _ in range(max_redirect):
191-
request = Request(url, verbose)
192-
yield from request.connect(pool)
193-
yield from request.send_request()
194-
response = yield from request.get_response()
195-
body = yield from response.read()
196-
next_url = response.get_redirect_url()
197-
if not next_url:
198-
break
199-
url = urllib.parse.urljoin(url, next_url)
200-
print('redirect to', url, file=sys.stderr)
201-
return body
194+
try:
195+
for _ in range(max_redirect):
196+
request = Request(url, verbose)
197+
yield from request.connect(pool)
198+
yield from request.send_request()
199+
response = yield from request.get_response()
200+
body = yield from response.read()
201+
next_url = response.get_redirect_url()
202+
if not next_url:
203+
break
204+
url = urllib.parse.urljoin(url, next_url)
205+
print('redirect to', url, file=sys.stderr)
206+
return body
207+
finally:
208+
pool.close()
202209

203210

204211
def main():

0 commit comments

Comments
 (0)
0