Skip to content

Commit

Permalink
Remove run_until_first_complete
Browse files Browse the repository at this point in the history
  • Loading branch information
uSpike committed May 13, 2021
1 parent 27283aa commit 3cce6a9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
11 changes: 0 additions & 11 deletions starlette/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@
T = typing.TypeVar("T")


async def run_until_first_complete(*args: typing.Tuple[typing.Callable, dict]) -> None:
async with anyio.create_task_group() as task_group:

async def task(_handler: typing.Callable, _kwargs: dict) -> Any:
await _handler(**_kwargs)
task_group.cancel_scope.cancel()

for handler, kwargs in args:
task_group.start_soon(task, handler, kwargs)


async def run_in_threadpool(
func: typing.Callable[..., T], *args: typing.Any, **kwargs: typing.Any
) -> T:
Expand Down
15 changes: 10 additions & 5 deletions starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import sys
import typing
from email.utils import formatdate
from functools import partial
from mimetypes import guess_type as mimetypes_guess_type
from urllib.parse import quote

import anyio

from starlette.background import BackgroundTask
from starlette.concurrency import iterate_in_threadpool, run_until_first_complete
from starlette.concurrency import iterate_in_threadpool
from starlette.datastructures import URL, MutableHeaders
from starlette.types import Receive, Scope, Send

Expand Down Expand Up @@ -216,10 +217,14 @@ async def stream_response(self, send: Send) -> None:
await send({"type": "http.response.body", "body": b"", "more_body": False})

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
await run_until_first_complete(
(self.stream_response, {"send": send}),
(self.listen_for_disconnect, {"receive": receive}),
)
async with anyio.create_task_group() as task_group:

async def wrap(coro: typing.Callable[..., typing.Awaitable]) -> None:
await coro()
task_group.cancel_scope.cancel()

task_group.start_soon(wrap, partial(self.stream_response, send))
task_group.start_soon(wrap, partial(self.listen_for_disconnect, receive))

if self.background is not None:
await self.background()
Expand Down
8 changes: 3 additions & 5 deletions tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from starlette import status
from starlette.concurrency import run_until_first_complete
from starlette.testclient import TestClient
from starlette.websockets import WebSocket, WebSocketDisconnect

Expand Down Expand Up @@ -222,10 +221,9 @@ async def writer(websocket):
async def asgi(receive, send):
websocket = WebSocket(scope, receive=receive, send=send)
await websocket.accept()
await run_until_first_complete(
(reader, {"websocket": websocket}),
(writer, {"websocket": websocket}),
)
async with anyio.create_task_group() as task_group:
task_group.start_soon(reader, websocket)
task_group.start_soon(writer, websocket)
await websocket.close()

return asgi
Expand Down

0 comments on commit 3cce6a9

Please sign in to comment.