Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a task per request handling. #3442

Merged
merged 6 commits into from
Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/3406.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create a task per request handling.
5 changes: 4 additions & 1 deletion aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,10 @@ async def start(self) -> None:
message, payload, self, writer, handler)
try:
try:
resp = await self._request_handler(request)
# a new task is used for copy context vars (#3406)
task = self._loop.create_task(
self._request_handler(request))
resp = await task
except HTTPException as exc:
resp = exc
except asyncio.CancelledError:
Expand Down
16 changes: 8 additions & 8 deletions tests/test_web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ async def test_simple(srv, buf) -> None:
srv.data_received(
b'GET / HTTP/1.1\r\n\r\n')

await asyncio.sleep(0)
await asyncio.sleep(0.01)
kxepal marked this conversation as resolved.
Show resolved Hide resolved
assert buf.startswith(b'HTTP/1.1 200 OK\r\n')


Expand Down Expand Up @@ -450,7 +450,7 @@ async def handle_request(request):
b'Content-Length: 50\r\n\r\n')
await asyncio.sleep(0)
assert not transport.close.called
await asyncio.sleep(0)
await asyncio.sleep(0.01)
transport.close.assert_called_with()


Expand All @@ -465,7 +465,7 @@ async def handle_request(request):
srv.connection_made(transport)
request_handler.side_effect = handle_request

await asyncio.sleep(0)
await asyncio.sleep(0.01)
assert not transport.close.called

srv.data_received(
Expand All @@ -475,7 +475,7 @@ async def handle_request(request):
await asyncio.sleep(0)
assert not transport.close.called

await asyncio.sleep(0)
await asyncio.sleep(0.01)
transport.close.assert_called_with()


Expand All @@ -490,7 +490,7 @@ async def test_handle_payload_access_error(
b'some data'
)
# start request_handler task
await asyncio.sleep(0)
await asyncio.sleep(0.01)

with pytest.raises(web.PayloadAccessError):
await request_handler.call_args[0][0].content.read()
Expand Down Expand Up @@ -582,7 +582,7 @@ async def test_keep_alive(make_srv, transport, ceil) -> None:
b'Host: example.com\r\n'
b'Content-Length: 0\r\n\r\n')

await asyncio.sleep(0)
await asyncio.sleep(0.01)
waiter = srv._waiter
assert waiter
assert srv._keepalive_handle is not None
Expand Down Expand Up @@ -681,7 +681,7 @@ async def test_close(srv, transport) -> None:
b'Host: example.com\r\n'
b'Content-Length: 0\r\n\r\n')

await asyncio.sleep(0)
await asyncio.sleep(0.01)
assert srv._task_handler
assert srv._waiter

Expand Down Expand Up @@ -721,7 +721,7 @@ async def handle(request):
assert len(srv._messages) == 2
assert srv._waiter is not None

await asyncio.sleep(0)
await asyncio.sleep(0.01)
assert srv._task_handler is not None
assert srv._waiter is not None
assert processed == 2
Expand Down