Skip to main content

iterlib

A library for easy iterator-based concurrency and parallelism.

Build Status
CircleCI

What is this?

Have you ever been working with map or a generator and gotten annoyed with how slow lazy evaluation made some tasks? Have you ever wondered "could I run this generator in the background?"

This library exists as the answer to that question. It implements asynchronous preloading generators and parallel map. Both the preloaders and the parallel map implementations support multiprocessing and threading as backends.

How do I use it?

Preload

Here's a simple example of preloading a generator:

from iterlib import thread_preload

gen = (x**2 for x in range(100000))
preloaded_gen = thread_preload(gen, buffer_size=100)

That's it! The generator will now preload up to 100 items in the background. When you call next(preloaded_gen), either directly or indirectly through a for statement, it will return values from the preloaded queue.

Parallel Map

Preloading generators has a significant limitation: it's impossible to use more than one background executor because access to iterators requires synchronization. However, most generators tend to be maps over other iterators, which opens an opportunity. We can't parallelize reads from an iterator, but we can parallelize function calls.

Use one of threaded_map or process_map when you know your generator is a map:

from iterlib import thread_map

gen = [x for x in range(100000)]
mapped_gen = thread_map(lambda x: x**2, buffer_size=100, num_workers=4)

This will create an ItemizedMap named mapped_gen. When you call iter(mapped_gen), a generator will be created in the background that will preload up to 100 samples per worker (so 400 total in this example).

Careful: These functions has different semantics than the regular Python map! If you map over an indexable collection (like a list or numpy array) the returned ItemizedMap will also be an indexable collection that lazily evaluates the map for each element you access! Only when iter is called (in a for loop or directly) will it return an asynchronous generator.

Release files for iterlib 1.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iterlib 1.1.6
File Size Uploaded
iterlib-1.1.6.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iterlib 1.1.6
File Interpreter ABI Platform
iterlib-1.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / iterlib-1.1.6.tar.gz

Download URL iterlib-1.1.6.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b6809624118651911367fdd2dc78927bb328396e0bbe06405b989aa064b942b7
BLAKE2b-256 checksum
How to use checksums
a87b698f91c993683dd2b0676f7bc3677caad386cf56a0255fb774245dae5e53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release files / iterlib-1.1.6-py3-none-any.whl

Download URL iterlib-1.1.6-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26a515972a8e16578b92bc5d8d058e122b740138da4be2e28222d97f81d2d617
BLAKE2b-256 checksum
How to use checksums
d59b0be2df11da154a218b63590125e85bbb8283cc55708a9801af9409011cd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release history Release notifications | RSS feed

This release

1.1.6 This release

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page