本文整理匯總了Python中typing.AsyncContextManager方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.AsyncContextManager方法的具體用法?Python typing.AsyncContextManager怎麽用?Python typing.AsyncContextManager使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing.AsyncContextManager方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_async_contextmanager
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def test_async_contextmanager(self):
class NotACM:
pass
self.assertIsInstance(ACM(), typing.AsyncContextManager)
self.assertNotIsInstance(NotACM(), typing.AsyncContextManager)
@contextlib.contextmanager
def manager():
yield 42
cm = manager()
self.assertNotIsInstance(cm, typing.AsyncContextManager)
self.assertEqual(typing.AsyncContextManager[int].__args__, (int,))
with self.assertRaises(TypeError):
isinstance(42, typing.AsyncContextManager[int])
with self.assertRaises(TypeError):
typing.AsyncContextManager[int, str]
示例2: aiohttp_request
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def aiohttp_request(method, url, **kwargs) -> typing.AsyncContextManager[aiohttp.ClientResponse]:
async with aiohttp.ClientSession() as session:
async with session.request(method, url, ssl=get_ssl_context(), **kwargs) as response:
yield response
示例3: test_iss_client_session
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def test_iss_client_session():
assert issubclass(requests.ISSClientSession, typing.AsyncContextManager)
async with requests.ISSClientSession() as session:
assert not session.closed
assert session.closed
示例4: __init__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def __init__(
self, fn: Callable[..., AsyncContextManager[EndpointAPI]], **kwargs: Any
) -> None:
functools.update_wrapper(self, fn)
self._fn = fn
self._kwargs = kwargs
示例5: __call__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def __call__(self, engine: EngineAPI) -> AsyncContextManager[EndpointAPI]:
return self._fn(engine, **self._kwargs)
#
# EndpointAPI.serve
#
示例6: _serve_endpoint
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def _serve_endpoint(
engine: EngineAPI, config: ConnectionConfig
) -> AsyncContextManager[EndpointAPI]:
logger.debug(
"[%s(%s)].serve(%s)", engine.endpoint_class.__name__, config.name, config.path
)
return engine.endpoint_class.serve(config)
示例7: _run_endpoint
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def _run_endpoint(engine: EngineAPI, name: str) -> AsyncContextManager[EndpointAPI]:
logger.debug("[%s(%s)].run()", engine.endpoint_class.__name__, name)
# EndpointAPI doesn't specify an __init__ so mypy doesn't understand this.
return engine.endpoint_class(name).run() # type: ignore
示例8: __init__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def __init__(self, context_managers: Sequence[AsyncContextManager[Any]]) -> None:
self.cms = tuple(context_managers)
self.cms_to_exit: Sequence[AsyncContextManager[Any]] = tuple()
示例9: _run_background_services
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def _run_background_services(
services: Sequence[ServiceAPI],
runner: Callable[[ServiceAPI], AsyncContextManager[ManagerAPI]]
) -> None:
async with contextlib.AsyncExitStack() as stack:
managers = tuple([
await stack.enter_async_context(runner(service))
for service in services
])
# If any of the services terminate, we do so as well.
await wait_first([
asyncio.create_task(manager.wait_finished())
for manager in managers
])
示例10: ParagonPeerPairFactory
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def ParagonPeerPairFactory(*,
alice_peer_context: ParagonContext = None,
alice_remote: NodeAPI = None,
alice_private_key: keys.PrivateKey = None,
alice_client_version: str = 'alice',
bob_peer_context: ParagonContext = None,
bob_remote: NodeAPI = None,
bob_private_key: keys.PrivateKey = None,
bob_client_version: str = 'bob',
event_bus: EndpointAPI = None,
) -> AsyncContextManager[Tuple[ParagonPeer, ParagonPeer]]:
if alice_peer_context is None:
alice_peer_context = ParagonContext()
if bob_peer_context is None:
bob_peer_context = ParagonContext()
return cast(AsyncContextManager[Tuple[ParagonPeer, ParagonPeer]], PeerPairFactory(
alice_peer_context=alice_peer_context,
alice_peer_factory_class=ParagonPeerFactory,
bob_peer_context=bob_peer_context,
bob_peer_factory_class=ParagonPeerFactory,
alice_remote=alice_remote,
alice_private_key=alice_private_key,
alice_client_version=alice_client_version,
bob_remote=bob_remote,
bob_private_key=bob_private_key,
bob_client_version=bob_client_version,
event_bus=event_bus,
))
示例11: get_connection
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import AsyncContextManager [as 別名]
def get_connection(self) -> AsyncContextManager[Connection]:
async with self._list_lock:
conn: Connection = await asyncio.shield(self._next_connection())
# The connection is locked so reconnections don't stack
async with conn.lock:
conn.users += 1
try:
yield conn
finally:
conn.users -= 1