Skip to main content

Functional interface for concurrent futures, including async coroutines.

Project description

image image image image build image CodeQL image ty

Futured provides a consistent interface for concurrent functional programming in Python. It wraps any callable to return a concurrent.futures.Future, wraps any async coroutine with a compatible Task interface, and provides concurrent iterators and context managers for futures.

Usage

threaded, processed

Transform any callable into one which runs in a thread or process pool, and returns a future.

import httpx2 as httpx
from futured import threaded, processed

fetch = threaded(httpx.Client().get)
fetch(url)  # return Future

fs = (fetch(url + path) for path in paths)
threaded.results(fs)  # generate results from futures
threaded.results(fs, timeout=...)  # generate results as completed

fetch.map(urls)  # generate results in order
fetch.map(urls, timeout=...)  # generate results as completed
fetch.mapzip(urls)  # generate (url, result) pairs as completed

Thread and process pool executors may be used as context managers, customized with options, and reused with different callables.

threaded(max_workers=...)(func, ...)
processed(max_workers=...)(func, ...)

futured classes provide a tasks interface which generalizes futures.as_completed and futures.wait, while allowing the set of tasks to be modified, e.g., for retries.

tasks = threaded.tasks(fs, timeout=...)  # mutable set of running tasks which pop as completed
with tasks:  # wait for all tasks on exit

asynced

The same interface works for asyncio.

from futured import asynced

fetch = asynced(httpx.AsyncClient().get)
fetch(url)  # return coroutine

asynced.results(fs)  # generate results from futures
asynced.results(fs, timeout=...)  # generate results as completed

fetch.map(urls)  # generate results in order
fetch.map(urls, timeout=...)  # generate results as completed
fetch.mapzip(urls)  # generate (url, result) pairs as completed

asynced provides utilities for calling coroutines from a synchronous context. tasks is similar to asyncio.TaskGroup, but in a synchronous with block.

asynced.run(async_func, ...)  # call and run until complete
asynced.run(async_gen, ...)  # call and run synchronous iterator
tasks = asynced.tasks(fs, timeout=...)  # mutable set of running tasks which pop as completed
with tasks:  # wait for all tasks on exit

extensions

There is also support for dask distributed clients and gevent greenlets.

from futured import distributed, greened

decorators

Naturally futured wrappers can be used as decorators, but arguments can also be partially bound.

@threaded
def slow(): ...


fetch = threaded(httpx.Client().get, url)
fetch(params=...)

Methods are supported, as well as a decorated utility for automatically subclassing.

from futured import decorated

FutureClient = decorated(httpx.Client, request=threaded)


# equivalent to
class FutureClient(httpx.Client):
    request = threaded(httpx.Client.request)

command

command wraps subprocess.Popen to provide a Future compatible interface.

from futured import futured, command

command('ls').result()  # return stdout or raises stderr
command('ls').pipe('wc')  # pipes into next command, or | ('wc',... )
for line in command('ls'):  # iterable lines
command.coroutine('ls')  # return coroutine

futured(command, 'ls')  # supports `map` interface
asynced(command.coroutine, 'ls')  # supports `map` interface with timeout

forked

forked allows iteration in separate child processes.

from futured import forked

for value in forked(values, max_workers=...):
    # in a child process
 # in parent after children have exited

Installation

pip install futured

Tests

100% branch coverage.

pytest [--cov]

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

futured-1.6.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

futured-1.6-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file futured-1.6.tar.gz.

File metadata

  • Download URL: futured-1.6.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for futured-1.6.tar.gz
Algorithm Hash digest
SHA256 16944fb183fd3b24cb3ea48965ad2046a68569cdbd4c5f8404a03aeaf26d01ae
MD5 b33269cdcaff11a68eb582fec8ea4921
BLAKE2b-256 f7474f599bfae7fd1533f4aef84a51d102eafc4df85584ff9477c6fb7c37dfcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for futured-1.6.tar.gz:

Publisher: release.yml on coady/futured

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file futured-1.6-py3-none-any.whl.

File metadata

  • Download URL: futured-1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for futured-1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef33846ae6258403e89fbfa6c30a4c6ebc2679561476fc052579fb093e3b9ae
MD5 762c5cff21bcb95e5032a4d43c55e588
BLAKE2b-256 63b561378f6021f7958223c13ecfbcc3bd2011a8a5430d1871fb324490c8bb07

See more details on using hashes here.

Provenance

The following attestation bundles were made for futured-1.6-py3-none-any.whl:

Publisher: release.yml on coady/futured

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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