Skip to main content

Parallel and concurrent iterators

Project description

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.

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

iterlib-1.1.6.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

iterlib-1.1.6-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file iterlib-1.1.6.tar.gz.

File metadata

  • Download URL: iterlib-1.1.6.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for iterlib-1.1.6.tar.gz
Algorithm Hash digest
SHA256 b6809624118651911367fdd2dc78927bb328396e0bbe06405b989aa064b942b7
MD5 a12167c343123dacb69f91dd1e0d6c1a
BLAKE2b-256 a87b698f91c993683dd2b0676f7bc3677caad386cf56a0255fb774245dae5e53

See more details on using hashes here.

File details

Details for the file iterlib-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: iterlib-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for iterlib-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 26a515972a8e16578b92bc5d8d058e122b740138da4be2e28222d97f81d2d617
MD5 ffd0bcaca53a02a9a1dbb2238f9a980b
BLAKE2b-256 d59b0be2df11da154a218b63590125e85bbb8283cc55708a9801af9409011cd5

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