Skip to main content

Asynchronous `zip` like aggregator for `async for`

Project description

# AsyncZip

`AsyncZip` is a simple `zip` like function working with asynchronous
iterables. It look, in many aspects, like `asyncio.wait`.

## Usage

Given *n* asynchronous iterables, `AsyncZip(*aiterables)` gives a new
asynchronous iterable iterating over all of them. Like `asyncio.wait`,
it's possible to wait for any iterable to yield something using the
option `FIRST_COMPLETED`, or to wait for all of them using the option
`ALL_COMPLETED`.

## class AsyncZip

def __init__(self, *asynchronous_iterables, loop=None,
yield_when=FIRST_COMPLETED):

- asynchronous_iterables: A collection of asynchronous iterables
- loop: Optional named parameter, to give an specific event loop
- yield_when: Like `return_when` from `asyncio.wait`,
`FIRST_COMPLETED` or `ALL_COMPLETED`.

## FIRST_COMPLETED, ALL_COMPLETED

The two values `yield_when` can take, `FIRST_COMPLETED` and
`ALL_COMPLETED` tell, like for `asyncio.wait`, when `AsyncZip` should
yield. Defaults to FIRST_COMPLETED.

### FIRST_COMPLETED

Like `select`, where `AsyncZip` yields each time any iterable yields,
as soon as possible.

### ALL_COMPLETED

It's the classical `zip` mode, where `AsyncZip` waits for a value for
each iterator before yielding them all in a single loop.

## Example usage

import asyncio
from asynczip import AsyncZip

class DummyAsyncIterable:
def __init__(self, items):
self.items = items

async def __aiter__(self):
return self

async def __anext__(self):
try:
return self.items.pop(0)
except IndexError:
raise StopAsyncIteration

async def test():
async for items in AsyncZip(DummyAsyncIterable(list("foo")),
DummyAsyncIterable(list("bar"))):
print(items)

asyncio.get_event_loop().run_until_complete(test())

Gives:

[<Task finished coro=<DummyAsyncIterable.__anext__() done, defined at test.py:11> result='f'>,<Task finished coro=<DummyAsyncIterable.__anext__() done, defined at test.py:11> result='b'>]
[<Task finished coro=<DummyAsyncIterable.__anext__() done, defined at test.py:11> result='o'>, <Task finished coro=<DummyAsyncIterable.__anext__() done, defined at test.py:11> result='a'>]
[<Task finished coro=<DummyAsyncIterable.__anext__() done, defined at test.py:11> result='o'>, <Task finished coro=<DummyAsyncIterable.__anext__() done, defined at test.py:11> result='r'>]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asynczip-1.0.tar.gz (1.6 kB view details)

Uploaded Source

File details

Details for the file asynczip-1.0.tar.gz.

File metadata

  • Download URL: asynczip-1.0.tar.gz
  • Upload date:
  • Size: 1.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for asynczip-1.0.tar.gz
Algorithm Hash digest
SHA256 56756abd2c960fede10da5a9a0390bb67940b27b3ccf0678719a04f877473f5d
MD5 2251c9e59951a057f8429b0fba6669e8
BLAKE2b-256 f01e9b745bcf9bf6ba912a727bb316b3617b468ca62de8eaea9f962142a0aba8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page