本文整理匯總了Python中inspect.isasyncgen方法的典型用法代碼示例。如果您正苦於以下問題:Python inspect.isasyncgen方法的具體用法?Python inspect.isasyncgen怎麽用?Python inspect.isasyncgen使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類inspect
的用法示例。
在下文中一共展示了inspect.isasyncgen方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: stream_until_disconnect
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isasyncgen [as 別名]
def stream_until_disconnect(
req: Request, source: Stream, raise_on_disconnect: bool
) -> Stream:
# Yield items from a stream until the client disconnects, then
# throw an exception into the stream (if told to do so).
assert inspect.isasyncgen(source)
async def stream():
async for item in source:
if not await req.is_disconnected():
yield item
continue
if raise_on_disconnect:
try:
await source.athrow(ClientDisconnect)
except StopAsyncIteration:
# May be raised in Python 3.6 if the `source`'s error
# handling code did not `yield` anything.
break
else:
break
return stream()
示例2: __init__
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isasyncgen [as 別名]
def __init__(self, iterable: Union[AsyncGenerator[bytes, None], Iterable]) -> None:
self.iter: AsyncGenerator[bytes, None]
if isasyncgen(iterable):
self.iter = iterable # type: ignore
elif isgenerator(iterable):
self.iter = run_sync_iterable(iterable) # type: ignore
else:
async def _aiter() -> AsyncGenerator[bytes, None]:
for data in iterable: # type: ignore
yield data
self.iter = _aiter()
示例3: format_stack
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isasyncgen [as 別名]
def format_stack(coro):
# Stop when reaching trio modules
if hasattr(coro, "cr_code"):
module = inspect.getmodule(coro.cr_code)
if module and module.__name__.startswith("trio."):
return
# Work around https://bugs.python.org/issue32810
if hasattr(coro, "__class__") and coro.__class__.__name__ in (
"async_generator_asend",
"async_generator_athrow",
):
coro, *_ = gc.get_referents(coro)
if not inspect.isasyncgen(coro):
return
# Follow a generator
if getattr(coro, "ag_frame", None):
yield from traceback.format_stack(coro.ag_frame)
yield from format_stack(coro.ag_await)
return
# Follow a coroutine
if getattr(coro, "cr_frame", None):
yield from traceback.format_stack(coro.cr_frame)
yield from format_stack(coro.cr_await)
return
# Follow a decorated coroutine
if getattr(coro, "gi_frame", None):
yield from traceback.format_stack(coro.gi_frame)
yield from format_stack(coro.gi_yieldfrom)
return
示例4: __anext__
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isasyncgen [as 別名]
def __anext__(self) -> Any:
if self.is_closed:
if not isasyncgen(self.iterator):
raise StopAsyncIteration
value = await self.iterator.__anext__()
result = self.callback(value)
else:
aclose = ensure_future(self._close_event.wait())
anext = ensure_future(self.iterator.__anext__())
pending: Set[Future] = (
await wait([aclose, anext], return_when=FIRST_COMPLETED)
)[1]
for task in pending:
task.cancel()
if aclose.done():
raise StopAsyncIteration
error = anext.exception()
if error:
if not self.reject_callback or isinstance(
error, (StopAsyncIteration, GeneratorExit)
):
raise error
result = self.reject_callback(error)
else:
value = anext.result()
result = self.callback(value)
return await result if isawaitable(result) else result
示例5: inspect_output_attrs
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isasyncgen [as 別名]
def inspect_output_attrs(obj):
is_awaitable = inspect.isawaitable(obj)
is_gen = inspect.isgenerator(obj)
is_async_gen = inspect.isasyncgen(obj)
return (is_awaitable, is_gen, is_async_gen)
示例6: smart_iter
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isasyncgen [as 別名]
def smart_iter(element):
if isasyncgen(element) or isasyncgenfunction(element):
async for x in element:
yield x
else:
for x in element:
yield x
示例7: async_transform
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isasyncgen [as 別名]
def async_transform(self, value, context=None):
if not isinstance(value, AbcMapping):
self._failure("value is not a dict", value=value, code=codes.IS_NOT_A_DICT)
collect = {}
errors = {}
touched_names = []
for key in self._keys:
key_run = getattr(key, 'async_call', key)(
value,
context=context,
)
if inspect.isasyncgen(key_run):
async for k, v, names in key_run:
if isinstance(v, DataError):
errors[k] = v
else:
collect[k] = v
touched_names.extend(names)
else:
for k, v, names in key_run:
if isinstance(v, DataError):
errors[k] = v
else:
collect[k] = v
touched_names.extend(names)
if not self.ignore_any:
for key in value:
if key in touched_names:
continue
if key in self.ignore:
continue
if not self.allow_any and key not in self.extras:
if key in collect:
errors[key] = DataError("%s key was shadowed" % key, code=codes.SHADOWED)
else:
errors[key] = DataError("%s is not allowed key" % key, code=codes.NOT_ALLOWED)
elif key in collect:
errors[key] = DataError("%s key was shadowed" % key, code=codes.SHADOWED)
else:
try:
collect[key] = await self.extras_trafaret.async_check(value[key])
except DataError as de:
errors[key] = de
if errors:
self._failure(error=errors, code=codes.SOME_ELEMENTS_DID_NOT_MATCH)
return collect