Skip to content

Commit

Permalink
ClientSession.close and Connector.close are coroutines now
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jan 25, 2016
1 parent 6cf7597 commit 5b72441
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ CHANGES
- Document ClientResponse's host, method, url properties

- Use CORK/NODELAY in client API #748

- ClientSession.close and Connector.close are coroutines now
5 changes: 4 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ def close(self):
if not self.closed:
self._connector.close()
self._connector = None
ret = asyncio.Future(loop=self._loop)
ret.set_result(None)
return ret

@property
def closed(self):
Expand Down Expand Up @@ -467,7 +470,7 @@ def __aenter__(self):

@asyncio.coroutine
def __aexit__(self, exc_type, exc_val, exc_tb):
self.close()
yield from self.close()

if PY_35:
from collections.abc import Coroutine
Expand Down
7 changes: 5 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,16 @@ def _start_cleanup_task(self):

def close(self):
"""Close all opened transports."""
ret = asyncio.Future(loop=self._loop)
ret.set_result(None)
if self._closed:
return
return ret
self._closed = True

try:
if hasattr(self._loop, 'is_closed'):
if self._loop.is_closed():
return
return ret

for key, data in self._conns.items():
for transport, proto, t0 in data:
Expand All @@ -245,6 +247,7 @@ def close(self):
self._conns.clear()
self._acquired.clear()
self._cleanup_handle = None
return ret

@property
def closed(self):
Expand Down
16 changes: 14 additions & 2 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,18 @@ The client session supports context manager protocol for self closing.

Add *origin* parameter.

.. method:: close()
.. coroutinemethod:: close()

Close underlying connector.

Release all acquired resources.

.. versionchanged:: 0.21

The method is converted into coroutine (but technically
returns a future for keeping backward compatibility during
transition period).

.. method:: detach()

Detach connector from session without closing the former.
Expand Down Expand Up @@ -706,10 +712,16 @@ BaseConnector

.. versionadded:: 0.16

.. method:: close()
.. coroutinemethod:: close()

Close all opened connections.

.. versionchanged:: 0.21

The method is converted into coroutine (but technically
returns a future for keeping backward compatibility during
transition period).

.. coroutinemethod:: connect(request)

Get a free connection from pool or create new one if connection
Expand Down

0 comments on commit 5b72441

Please sign in to comment.