Skip to main content

No project description provided

Project description

Five functions in itertools can be substantially slowed when their input iterables generate data with lag. This might occur when the generator yields data being streamed from the internet, disk, or a multithreaded or multiprocessing workflow.

The most substantial slowdowns are in permutations, combinations, combinations_with_replacement, and product. These block the flow of execution until their input iterable(s) have completely finished being converted to a tuple. Example:

import time
import itertools
import nonblocking_itertools

def laggy_generator(iterable):
    for i in iterable:
        time.sleep(1)
        yield i

print("Testing itertools.combinations")
start = time.time()
data = laggy_generator(range(4))
for combo in itertools.combinations(data, 2):
    print("Seconds from start:", int(time.time() - start), "Combination:", combo)

print("Testing nonblocking_itertools.combinations")
start = time.time()
data = laggy_generator(range(4))
for combo in nonblocking_itertools.combinations(data, 2):
    print("Seconds from start:", time.time() - start, "Combination:", combo)

Output:

Testing itertools.combinations
Seconds from start: 4 Combination: (0, 1)
Seconds from start: 4 Combination: (0, 2)
Seconds from start: 4 Combination: (0, 3)
Seconds from start: 4 Combination: (1, 2)
Seconds from start: 4 Combination: (1, 3)
Seconds from start: 4 Combination: (2, 3)
Testing nonblocking_itertools.combinations
Seconds from start: 2 Combination: (0, 1)
Seconds from start: 3 Combination: (0, 2)
Seconds from start: 3 Combination: (1, 2)
Seconds from start: 4 Combination: (0, 3)
Seconds from start: 4 Combination: (1, 3)
Seconds from start: 4 Combination: (2, 3)

Notice that all results from itertools.combinations were printed after 4 seconds, while the results from nonblocking_itertools.combinations were printed as soon as it was possible to yield them, after 2 or 3 seconds. Note that nonblocking_itertools does not generally return results in the same order as the equivalent itertools methods.

The other improvement in nonblocking_itertools is with the chain method, for when iterables have some parallelizeable processing that is initiated only during iteration. The itertools.chain method only starts iterating through later iterables when the first is exhausted. That means it misses the opportunity to kick off parallel processing in the later iterables. By contrast, nonblocking_itertools initiates immediate and simultaneous multithreaded iteration through iterables to take advantage of opportunities for parallelization. Its output is identical to itertools.chain.

Example:

import time
import itertools
import nonblocking_itertools

def laggy_generator(iterable):
    for i in iterable:
        time.sleep(1)
        yield i

print("Testing itertools.chain")
start = time.time()
data1 = laggy_generator(range(2))
data2 = laggy_generator(range(2, 4))
for element in itertools.chain(data1, data2):
    print("Seconds from start:", int(time.time() - start), "Element:", element)

print("Testing nonblocking_itertools.chain")
start = time.time()
data1 = laggy_generator(range(2))
data2 = laggy_generator(range(2, 4))
for element in nonblocking_itertools.chain(data1, data2):
    print("Seconds from start:", int(time.time() - start), "Element:", element)

Output:

Testing itertools.chain
Seconds from start: 1 Element: 0
Seconds from start: 2 Element: 1
Seconds from start: 3 Element: 2
Seconds from start: 4 Element: 3
Testing nonblocking_itertools.chain
Seconds from start: 1 Element: 0
Seconds from start: 2 Element: 1
Seconds from start: 2 Element: 2
Seconds from start: 2 Element: 3

Notice that nonblocking_itertools.chain finishes after 2 seconds, while itertools.chain takes 4 seconds to finish.

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

nonblocking-itertools-0.1.2.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

nonblocking_itertools-0.1.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file nonblocking-itertools-0.1.2.tar.gz.

File metadata

  • Download URL: nonblocking-itertools-0.1.2.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for nonblocking-itertools-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9cd8a3bf3504da217fc0b73a3c36827f017d832eb266d9e8ece6eaa001fa2d86
MD5 ae1040f7ef7badc318dfda79f9befb4b
BLAKE2b-256 7ddc4963e3cd1c1e1323bdeb673acff21681b5167fbdb9db6242ea1e393b152a

See more details on using hashes here.

File details

Details for the file nonblocking_itertools-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for nonblocking_itertools-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d2cf8b472aeebb3b657a8849a2a5aa2b715678e2ac27a5578c9e46646deb33be
MD5 7a2419eb0062d9892e1d4947bbde3d81
BLAKE2b-256 f77e1ba13e0637033529caa5753470a59e7d5fe45216975584bd908594101852

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