Skip to main content

Parallelize pipelines of Python async iterables/generators

Project description

asyncio-buffered-pipeline CircleCI Test Coverage

Parallelise pipelines of Python async iterables/generators.

Installation

pip install asyncio-buffered-pipeline

Usage / What problem does this solve?

If you have a pipeline of async generators, even though each is async, only one runs at any given time. For example, the below runs in (just over) 30 seconds.

import asyncio

async def gen_1():
    for value in range(0, 10):
        await asyncio.sleep(1)  # Could be a slow HTTP request
        yield value

async def gen_2(it):
    async for value in it:
        await asyncio.sleep(1)  # Could be a slow HTTP request
        yield value * 2

async def gen_3(it):
    async for value in it:
        await asyncio.sleep(1)  # Could be a slow HTTP request
        yield value + 3

async def main():
    it_1 = gen_1()
    it_2 = gen_2(it_1)
    it_3 = gen_3(it_2)

    async for val in it_3:
        print(val)

asyncio.run(main())

The buffered_pipeline function allows you to make to a small change, passing each generator through its return value, to parallelise the generators to reduce this to (just over) 12 seconds.

import asyncio
from asyncio_buffer_iterable import buffered_pipeline

async def gen_1():
    for value in range(0, 10):
        await asyncio.sleep(1)  # Could be a slow HTTP request
        yield value

async def gen_2(it):
    async for value in it:
        await asyncio.sleep(1)  # Could be a slow HTTP request
        yield value * 2

async def gen_3(it):
    async for value in it:
        await asyncio.sleep(1)  # Could be a slow HTTP request
        yield value + 3

async def main():
    buffer_iterable = buffered_pipeline()
    it_1 = buffer_iterable(gen_1())
    it_2 = buffer_iterable(gen_2(it_1))
    it_3 = buffer_iterable(gen_3(it_2))

    async for val in it_3:
        print(val)

asyncio.run(main())

The buffered_pipeline ensures internal tasks are cleaned up on any exception.

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

asyncio-buffered-pipeline-0.0.2.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file asyncio-buffered-pipeline-0.0.2.tar.gz.

File metadata

  • Download URL: asyncio-buffered-pipeline-0.0.2.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for asyncio-buffered-pipeline-0.0.2.tar.gz
Algorithm Hash digest
SHA256 71959b977a7dfac4ea6710890891493e54ddf8a026259f6ad0d133ad96f9ef2c
MD5 777d4e725628cf455d2d4f00b30a5a29
BLAKE2b-256 b69c638692e15b8ef33ca336e665c14cfdf45364e4cacff0030a22d6927a0a8b

See more details on using hashes here.

File details

Details for the file asyncio_buffered_pipeline-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: asyncio_buffered_pipeline-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for asyncio_buffered_pipeline-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b94014152f991ff4781a9b00c75a0a5d4ac26b1dbf2b28a8ff4f47c9aa5427cc
MD5 7ae2025135692876ec84ef9c81a43929
BLAKE2b-256 412901066ba2c83ada7bb8312e1fb31c90ccb3b310082a1aa50ebb8d25c2e50e

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