src/pylib/collections_abc/asyncs

Source   Edit  

Coroutine

ref pep of `Coroutines with async and await syntax`, Future-like object is just an alias of Awaitable

V = 1
async def f():
    return V

c: Coroutine[None, None, int] = f()

try: c.send(None)
except StopIteraion as s: assert V == s.value

try: c.send(None)
except RuntimeError as e: assert str(e) == "cannot reuse already awaited coroutine"

# Also: V == await f()

pep for Asynchronous Generators

AsyncGenerator

V = 1
async def f(): yield V
ag = f()

ag_asend_obj = ag.asend(None)  #  or `anext(ag)`
try: ag_asend_obj.send(None)
except StopIteration as s: assert V == s.value


ag_asend_obj = ag.asend(None)  #  or `anext(ag, defval)` if wanting
# the next `send` raises `StopIteration` and its value to be `defval`

try: ag_asend_obj.send(None)
except StopAsyncIteration: pass   # iteration end

for ag_asend_obj, ref link of PyAsyncGenASend:

PyAsyncGenASend is a coroutine-like object...

PyAsyncGenAThrow is very similar to PyAsyncGenASend. The only difference is that PyAsyncGenAThrow.send(), when called first time, throws an exception into the parent agen object (instead of pushing a value into it.)

Types

AsyncGenerator[Yield; Send] = concept selfof AsyncIterator[Yield]
    self.asend(Send) is Awaitable[Yield]
    self.athrow(CatchableError) is Awaitable[void]
type( (async _(): yield)() ) ABC for such a function which returns an asynchronous generator iterator. It looks like a coroutine function defined with async def except that it contains yield expressions for producing a series of values usable in an async for loop. Source   Edit  
AsyncIterable[T] = concept self
    aiter(self) is AsyncIterator[T]
Source   Edit  
AsyncIterator[T] = concept selfof AsyncIterable[T]
    anext(self) is Awaitable[T]
Source   Edit  
Awaitable[T] = concept self
    T is await self
Source   Edit  
Coroutine[Yield; Send; Return] = concept selfof Awaitable[Return]
    self.send(Send) is Yield
    self.throw(CatchableError)
type( (async def _(): return Return() )() ) in details: Coroutine[None, None, Return] Source   Edit  

Procs

proc aclose(self: AsyncGenerator): owned(Future[void]) {....stackTrace: false.}
Source   Edit  
proc anext[T](self: AsyncGenerator[T]): Awaitable[T] {.inline.}
this shall be an async def Source   Edit  
proc anext[T](self: AsyncGenerator[T]; default: T): Awaitable[T] {.inline.}
Source   Edit  
proc await[T](self: anextawaitableobject[T]): T
Source   Edit  
proc close(self: Coroutine)
Source   Edit