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.0.tar.gz (2.6 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.0-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file nonblocking_itertools-0.1.0.tar.gz.

File metadata

  • Download URL: nonblocking_itertools-0.1.0.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/7.0.1 keyring/24.3.0 pkginfo/1.9.6 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.31.0 rfc3986/1.5.0 tqdm/4.66.1 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for nonblocking_itertools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6dd627f7a8f1eb1181aa52015ce5e97ad7b1975cd966085ebf8b6a37a64c4861
MD5 0f2692362cc383a7be6bead4da264470
BLAKE2b-256 810c9f63e743407723bc4d7cdab9188a09d668400a7a8565a5d56dc4c4a55f50

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nonblocking_itertools-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/7.0.1 keyring/24.3.0 pkginfo/1.9.6 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.31.0 rfc3986/1.5.0 tqdm/4.66.1 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for nonblocking_itertools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6d99c2569de083eb985f7749eabae10e855c33ff8efe0c61674e069b0f44e4d
MD5 889f7cb9b03f22c68b8969ed3a302e16
BLAKE2b-256 00bb7af9bc3e1ca10a50ae7114e3328aafeeec1112501908b96104f1cdcb392e

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