File size: 1,321 Bytes
4cef980 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from typing import Optional
import sniffio
from .base import AsyncNetworkBackend, AsyncNetworkStream
class AutoBackend(AsyncNetworkBackend):
async def _init_backend(self) -> None:
if not (hasattr(self, "_backend")):
backend = sniffio.current_async_library()
if backend == "trio":
from .trio import TrioBackend
self._backend: AsyncNetworkBackend = TrioBackend()
else:
from .asyncio import AsyncIOBackend
self._backend = AsyncIOBackend()
async def connect_tcp(
self,
host: str,
port: int,
timeout: Optional[float] = None,
local_address: Optional[str] = None,
) -> AsyncNetworkStream:
await self._init_backend()
return await self._backend.connect_tcp(
host, port, timeout=timeout, local_address=local_address
)
async def connect_unix_socket(
self, path: str, timeout: Optional[float] = None
) -> AsyncNetworkStream: # pragma: nocover
await self._init_backend()
return await self._backend.connect_unix_socket(path, timeout=timeout)
async def sleep(self, seconds: float) -> None: # pragma: nocover
await self._init_backend()
return await self._backend.sleep(seconds)
|