Python inspect.isasyncgen() Examples
The following are 7
code examples of inspect.isasyncgen().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
inspect
, or try the search function
.
Example #1
Source File: streaming.py From bocadillo with MIT License | 6 votes |
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()
Example #2
Source File: response.py From quart with MIT License | 5 votes |
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()
Example #3
Source File: monitoring.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
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
Example #4
Source File: map_async_iterator.py From graphql-core with MIT License | 5 votes |
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
Example #5
Source File: kernel.py From reactivepy with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #6
Source File: helpers.py From vibora with MIT License | 5 votes |
def smart_iter(element): if isasyncgen(element) or isasyncgenfunction(element): async for x in element: yield x else: for x in element: yield x
Example #7
Source File: async_mixins.py From trafaret with BSD 2-Clause "Simplified" License | 4 votes |
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