Skip to main content

Limits the number of values generated by an iterator until acknowledged.

Project description

Run Status Coverage Badge Supported Python versions License

Intro

multiprocessing.pool.ThreadPool and multiprocessing.Pool have the wonderful imap and imap_unordered methods which allow to consume an iterable incrementally and will yield results as they become available.

This is wonderful as it allows to process inputs or results larger than available memory.

It has an important flaw which is that it does not limit the number of results that are not consumed. This causes it to use memory without bound.

This package implements a wrapper over an iterable which will limit the number of results generated until they are acknowledged. It allows a number of messages to be processed concurrently but no more than that.

Usage

it = itertools.count()           # The input iterable.
it = BoundedIterator(10, it)     # Allows concurrent processing of up to 10 values.
results = Pool().imap(str, it)   # Will begin consuming `it` and producing results.
time.sleep(5)                    # No matter what else we do, no more than 10 results will be available.
for res in results:              # Consume normally.
  print(res)
  it.processed()                 # Acknowledge a value was processed so that a new one can be generated.

Development

Setup pre-commit hooks and you are ready to go.

pre-commit install --install-hooks

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

bounded_iterator-1.0.0-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file bounded_iterator-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: bounded_iterator-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for bounded_iterator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cba6c281e8f53d80be0baf8e4d4225509164b22f251eb665caf89580a6b84c01
MD5 9d465616234d61c9bdc2a9b1d27eda42
BLAKE2b-256 fdd6cabf57e9ed9ff0bea47fbaeb2e5752e0821b163649d9fdd4765c2553c0b3

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