Skip to main content

A concurrent.futures.Executor implementation that performs sequential execution

Project description

sequential-executor

A concurrent.futures.Executor implementation that performs sequential execution.

While this micro-library may seem a little oxymoronic (Executors are, after all, meant to execute calls asynchronously), it does have a fairly well-defined use case.

Use case

A Python package that performs multiple operations via callback(s) defined by implementors using that package. The package allows the implementor to define the concurrent execution environment by passing in a concurrent.futures.Executor object.

This allows the implementor to tailor the execution environment based upon the work that their callback will be performing. For example, if they are making web calls (e.g. - IO bound) they could pass in a ThreadPoolExecutor, but if they are doing numpy analysis of large datasets (e.g. - CPU bound), they could pass in a ProcessPoolExecutor. But what if the implementor must enforce sequential execution, such as for ordered message processing?

The SequentialExecutor provided by this package allows for the implementor using the hypothetical Python package to give that package an Executor, but forces the execution environment into seqential ordering.

Installation

pip install sequential-executor

SequentialExecutor Example

import concurrent.futures
import urllib.request

import sequential_executor

URLS = ['http://www.foxnews.com/',
        'http://www.cnn.com/',
        'http://europe.wsj.com/',
        'http://www.bbc.co.uk/',
        'http://some-made-up-domain.com/']

# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
    print('loading %r' % url)
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

# We can use in a with statement just like other Executors
with sequential_executor.SequentialExecutor() as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r page is %d bytes' % (url, len(data)))

NOTE - Although this looks like it is submitting multiple jobs and then waiting on each job to complete, the concurrent.futures.Future objects returned by the SequentialExecutor will be fully complete upon the return from the submit call!

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

sequential-executor-1.0.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

sequential_executor-1.0.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file sequential-executor-1.0.0.tar.gz.

File metadata

  • Download URL: sequential-executor-1.0.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for sequential-executor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 703b7af8abc595d69a6371345ad4d8092dafba1b30d544c05b27735302d1db18
MD5 331bd659a21167fa6fd5f710c1d2bb37
BLAKE2b-256 83f26f514bde07e85dbf473954e78a0e6d23cb1dee2cb19e0f6dcb675c1e63be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sequential_executor-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for sequential_executor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56054125c357c6915137e2cde6ec5c1fba102922ad9433b422cdcc0b1f01234a
MD5 451030ac585cb1d2a056ba227e334101
BLAKE2b-256 4e2f015b7e3e5274027bb740ed0dcdccb0c35076c344188cd10dd341da46b169

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