Skip to content

Commit

Permalink
make _portably_async_sleep a sync function that returns an async func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
jakkdl committed Jun 13, 2024
1 parent 45c7cf5 commit 7106f54
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions tenacity/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
WrappedFn = t.TypeVar("WrappedFn", bound=t.Callable[..., t.Awaitable[t.Any]])


async def _portable_async_sleep(seconds: float) -> None:
def _portable_async_sleep(seconds: float) -> t.Awaitable[None]:
# If trio is already imported, then importing it is cheap.
# If trio isn't already imported, then it's definitely not running, so we
# can skip further checks.
Expand All @@ -40,13 +40,12 @@ async def _portable_async_sleep(seconds: float) -> None:
import sniffio

if sniffio.current_async_library() == "trio":
await trio.sleep(seconds)
return
return trio.sleep(seconds)
# Otherwise, assume asyncio
# Lazy import asyncio as it's expensive (responsible for 25-50% of total import overhead).
import asyncio

await asyncio.sleep(seconds)
return asyncio.sleep(seconds)


class AsyncRetrying(BaseRetrying):
Expand Down

0 comments on commit 7106f54

Please sign in to comment.