This Python package is intended as an add-on to aioitertools
, filling some gaps in its API. The aim of the project is to eventually provide async versions of all builtins and everything in more_itertools
. It currently provides:
- The class
Queue
which is both a subclass ofasyncio.Queue
and an async iterator. - The
collate
async iterator, which is an async variant ofheapq.merge
(akamore_itertools.collate
). Currently does not supportreverse
but does supportkey
. - The
consume
coroutine, which is an async variant ofmore_itertools.consume
. It takes an iterable (sync or async) and an optional numbern
(defaults to infinity), and advances it byn
steps, dropping the yielded values. If the iterable has fewer thann
items remaining, it is consumed entirely, no exception is raised. - The
merge
async iterator, which takes an iterable of iterables, and yields items from them in the order they arrive. See the example. Both the outer iterable and the inner iterables may be sync or async. - The
never
async iterator, which never yields or returns. - The
tuple
coroutine, which takes an iterable and collects it into a tuple, similar toaioitertools.list
andaioitertools.set
. - The
unique_justseen
async iterator, which is an async variant ofmore_itertools.unique_justseen
. It takes an iterable (sync or async) and an optional functionkey
(defaults to identity), and yields only those items from the iterable where the result ofkey
is different from that of the previously yielded item. - The
wait
async iterator, which takes an iterable (sync or async) of futures, and yields results from the front of the iterable as soon as they become available. Using this is similar to callingasyncio.wait
inreturn_when=ALL_COMPLETED
mode and taking the first return value, except it can handle an empty iterable,.result()
does not have to be called, futures near the start can be used before later futures become available, and futures yielded from the iterable are started before continuing the iteration.
Python 3.7 is required. For a version compatible with Python 3.6, see the py36 branch.
>>> import aitertools
>>> import asyncio
>>> import more_aitertools
>>> async def slow_count(seconds):
... async for i in aitertools.count():
... yield i
... await asyncio.sleep(seconds)
...
>>> async def slow_counts(limit):
... for seconds in range(1, limit + 1):
... yield slow_count(seconds)
...
>>> sync(aitertools.alist(aitertools.islice(more_aitertools.merge(slow_counts(3)), 10)))
[0, 0, 0, 1, 1, 2, 3, 1, 2, 4]