Skip to content

Commit 8e2bc3e

Browse files
Merge pull request #109 from pusher/aiohttp-cleanup
use an explicit session and close after request
2 parents d90c4bc + a8327cf commit 8e2bc3e

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

pusher/aiohttp.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ def __init__(self, client):
1919
:param client: pusher.Client object
2020
"""
2121
self.client = client
22-
self.conn = aiohttp.TCPConnector()
2322

2423

24+
@asyncio.coroutine
2525
def send_request(self, request):
26-
method = request.method
27-
url = "%s%s" % (request.base_url, request.path)
28-
params = request.query_params
29-
data = request.body
30-
headers = request.headers
31-
32-
response = yield from asyncio.wait_for(
33-
aiohttp.request(
34-
method, url, params=params, data=data, headers=headers,
35-
connector=self.conn),
36-
timeout=self.client.timeout)
37-
38-
body = yield from response.text('utf-8')
39-
return process_response(response.status, body)
26+
session = response = None
27+
try:
28+
session = aiohttp.ClientSession()
29+
response = yield from session.request(
30+
request.method,
31+
"%s%s" % (request.base_url, request.path),
32+
params=request.query_params,
33+
data=request.body,
34+
headers=request.headers,
35+
timeout=self.client.timeout
36+
)
37+
body = yield from response.text('utf-8')
38+
return body
39+
finally:
40+
if response is not None:
41+
response.close()
42+
if session is not None:
43+
session.close()

0 commit comments

Comments
 (0)