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

Starlette DeprecationWarning: run_until_first_complete is deprecated and will be removed in a future version. #26

Closed
florimondmanca opened this issue Nov 13, 2022 · 0 comments · Fixed by #27

Comments

@florimondmanca
Copy link
Owner

florimondmanca commented Nov 13, 2022

We get a warning when running tests:

tests/test_example.py::test_example
  /Users/florimond/Developer/florimondmanca-projects/arel/venv/lib/python3.11/site-packages/starlette/concurrency.py:19: DeprecationWarning: run_until_first_complete is deprecated and will be removed in a future version.
    warnings.warn(

As per encode/starlette#1443, the alternative seems to be to use anyio:

async def run_until_first_complete(*args: typing.Tuple[typing.Callable, dict]) -> None:
    async def run(handler, kwargs):
        await handler(**kwargs)
        tg.cancel_scope.cancel()

    async with anyio.create_task_group() as tg:
        for handler, kwargs in args:
            tg.start_soon(run, handler, kwargs)

We only support asyncio for now (see usage of asyncio.Event()). So as a first step, we could resolve the warning by bringing back what Starlette was doing before anyio...

async def run_until_first_complete(*args: typing.Tuple[typing.Callable, dict]) -> None:
    tasks = [create_task(handler(**kwargs)) for handler, kwargs in args]
    (done, pending) = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
    [task.cancel() for task in pending]
    [task.result() for task in done]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant