My code is as follows. I want the two sleep can share the same time frame and take 1+2*3=7 seconds to run the script. But it seems that something wrong happened so that it still takes 3*(1+2) second.
Is there any idea how to modify the code?
import asyncio
async def g():
for i in range(3):
await asyncio.sleep(1)
yield i
async def main():
async for x in g():
print(x)
await asyncio.sleep(2)
loop = asyncio.get_event_loop()
res = loop.run_until_complete(main())
loop.close()
Simply put these are generators written in asynchronous functions (instead of def function(..) they use async def function(..)
Async generators (finally) For most practical applications, when we'd like to make an object that asynchronously generates a sequence of values, we can use an asynchronous generator. The syntax is simple: prepend function* with async . That makes the generator asynchronous.
There are two basic types of methods in the Parallels Python API: synchronous and asynchronous. When a synchronous method is invoked, it completes executing before returning to the caller. An asynchronous method starts a job in the background and returns to the caller immediately.
An __aiter__ method returning an asynchronous iterator. An __anext__ method returning an awaitable object, which uses StopIteration exception to “yield” values, and StopAsyncIteration exception to signal the end of the iteration.
The point of async
/await
is to interleave tasks, not functions/generators. For example, when you await asyncio.sleep(1)
, your current coroutine is delayed along with the sleep. Similarly, an async for
delays its coroutine until the next item is ready.
In order to run your separate functionality, you must create each part as a separate task. Use a Queue
to exchange items between them - tasks will only be delayed until they have exchanged an item.
from asyncio import Queue, sleep, run, gather
# the original async generator
async def g():
for i in range(3):
await sleep(1)
yield i
async def producer(queue: Queue):
async for i in g():
print('send', i)
await queue.put(i) # resume once item is fetched
await queue.put(None)
async def consumer(queue: Queue):
x = await queue.get() # resume once item is fetched
while x is not None:
print('got', x)
await sleep(2)
x = await queue.get()
async def main():
queue = Queue()
# tasks only share the queue
await gather(
producer(queue),
consumer(queue),
)
run(main())
If you regularly need this functionality, you can also put it into a helper object that wraps an asynchronous iterable. The helper encapsulates the queue and separate task. You can apply the helper directly on an async iterable in an async for
statement.
from asyncio import Queue, sleep, run, ensure_future
# helper to consume iterable as concurrent task
async def _enqueue_items(async_iterable, queue: Queue, sentinel):
async for item in async_iterable:
await queue.put(item)
await queue.put(sentinel)
async def concurrent(async_iterable):
"""Concurrently fetch items from ``async_iterable``"""
queue = Queue()
sentinel = object()
consumer = ensure_future( # concurrently fetch items for the iterable
_enqueue_items(async_iterable, queue, sentinel)
)
try:
item = await queue.get()
while item is not sentinel:
yield item
item = await queue.get()
finally:
consumer.cancel()
# the original generator
async def g():
for i in range(3):
await sleep(1)
yield i
# the original main - modified with `concurrent`
async def main():
async for x in concurrent(g()):
print(x)
await sleep(2)
run(main())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With