Skip to main content

Parallelize pipelines of Python iterables

Project description

threaded-buffered-pipeline CircleCI Test Coverage

Parallelise pipelines of Python iterators.

Installation

pip install threaded-buffered-pipeline

Usage / What problem does this solve?

If you have a chain of generators, only one runs at any given time. For example, the below runs in (just over) 30 seconds.

import time

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

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

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

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

    for val in it_3:
        print(val)

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 time
from threaded_buffered_pipeline import buffered_pipeline

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

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

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

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))

    for val in it_3:
        print(val)

main()

The buffered_pipeline ensures internal threads are stopped on any exception [the next time each thread attempts to pull from the iterator].

Buffer size

The default buffer size is 1. This is suitable if each iteration takes approximately the same amount of time. If this is not the case, you may wish to change it using the buffer_size parameter of buffer_iterable.

it = buffer_iterable(gen(), buffer_size=2)

Features

  • One thread is created for each buffer_iterable, in which the iterable is iterated over, with its values stored in an internal buffer.

  • All the threads of the pipeline are stopped if any of the generators raise an exception.

  • If a generator raises an exception, the exception is propagated to calling code.

  • The buffer size of each step in the pipeline is configurable.

  • The "chaining" is not abstracted away. You still have full control over the arguments passed to each step, and you don't need to buffer each iterable in the pipeline if you don't want to: just don't pass those through buffer_iterable.

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

threaded-buffered-pipeline-0.0.6.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

threaded_buffered_pipeline-0.0.6-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file threaded-buffered-pipeline-0.0.6.tar.gz.

File metadata

  • Download URL: threaded-buffered-pipeline-0.0.6.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for threaded-buffered-pipeline-0.0.6.tar.gz
Algorithm Hash digest
SHA256 d18b444bd6cf6423084f023ea78792ef7841897eea22e92b4cc8bbc7d479058c
MD5 1c266f4f106c5416689dfa5cf2bee6cc
BLAKE2b-256 1a59597b1213c2168fa345c7e636706c01216e5dddb4db5f2198d2506fe4bd19

See more details on using hashes here.

File details

Details for the file threaded_buffered_pipeline-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: threaded_buffered_pipeline-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.6 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/50.3.2 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for threaded_buffered_pipeline-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ea54715dc6133ad359b9e3e9ded21edee5ef3f3b8d3e8cfa23416148ebf66e9b
MD5 432f811fc72bf3fc751ed4b12ed6b3e4
BLAKE2b-256 6b53f67217a5a746d866bee4a55373b242a0923c044b6fd5103ad4085b140300

See more details on using hashes here.

Supported by

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