Functional interface for concurrent futures, including async coroutines.
Project description
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.
from futured import threaded, processed
import httpx
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 have a waiting context manager which collects results from tasks. Futures can be registered at creation, or appended to the list of tasks.
with threaded.waiting(*fs) as tasks:
tasks.append(future)
tasks # list of completed results
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.
threaded.tasks(fs, timeout=...) # mutable set of running tasks which iterate as completed
asynced
The same interface works for asyncio.
from futured import asynced
import httpx
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. waiting is similar to trio's nursery, but returns results from a synchronous with block.
asynced.run(async_func, ...) # call and run until complete
asynced.run(async_gen, ...) # call and run synchronous iterator
with asynced.waiting(*fs) as tasks: # concurrent coroutines completed in a block
asynced.tasks(fs, timeout=...) # mutable set of running tasks which iterate as completed
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file futured-1.5.1.tar.gz.
File metadata
- Download URL: futured-1.5.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e579bcc8697bc32809a69c30a3b16d4f0b235be44dc4728c4581e82b14deb277
|
|
| MD5 |
224a20dd2faf763c338d9dafc6f7e1eb
|
|
| BLAKE2b-256 |
8dc9402b65d2667f2660a396a77b9ec73ac84314020a9c242d73967cec055d2b
|
Provenance
The following attestation bundles were made for futured-1.5.1.tar.gz:
Publisher:
release.yml on coady/futured
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
futured-1.5.1.tar.gz -
Subject digest:
e579bcc8697bc32809a69c30a3b16d4f0b235be44dc4728c4581e82b14deb277 - Sigstore transparency entry: 660748708
- Sigstore integration time:
-
Permalink:
coady/futured@0cf8504884f221c703a321e12e41142eec21c35b -
Branch / Tag:
refs/tags/v1.5.1 - Owner: https://github.com/coady
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0cf8504884f221c703a321e12e41142eec21c35b -
Trigger Event:
push
-
Statement type:
File details
Details for the file futured-1.5.1-py3-none-any.whl.
File metadata
- Download URL: futured-1.5.1-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.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9feb49683e6ce0223ea71ffffd7e7a77df34b5e6e43c5149b77001f01af74ac
|
|
| MD5 |
5612daf9d176393ad30b4261c1a6df47
|
|
| BLAKE2b-256 |
91e2e6899411f003e43346a3440b7d255f2182cade23b69c64d2cc98e250b41a
|
Provenance
The following attestation bundles were made for futured-1.5.1-py3-none-any.whl:
Publisher:
release.yml on coady/futured
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
futured-1.5.1-py3-none-any.whl -
Subject digest:
e9feb49683e6ce0223ea71ffffd7e7a77df34b5e6e43c5149b77001f01af74ac - Sigstore transparency entry: 660748709
- Sigstore integration time:
-
Permalink:
coady/futured@0cf8504884f221c703a321e12e41142eec21c35b -
Branch / Tag:
refs/tags/v1.5.1 - Owner: https://github.com/coady
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0cf8504884f221c703a321e12e41142eec21c35b -
Trigger Event:
push
-
Statement type: