Skip to main content

Functional interface for concurrent futures, including asynchronous I/O.

Project description

https://img.shields.io/pypi/v/futured.svg https://img.shields.io/pypi/pyversions/futured.svg https://img.shields.io/pypi/status/futured.svg https://img.shields.io/travis/coady/futured.svg https://img.shields.io/codecov/c/github/coady/futured.svg

Futured provides a simple consistent interface for concurrent functional programming in Python. It can wrap any callable to return concurrent.futures.Future objects, and it can wrap any async coroutine to return asyncio.Future objects.

Usage

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

from futured import threaded, processed
import requests

fetch = threaded(requests.Session().get)
fetch(url)  # returns Future

fs = (fetch(url + path) for path in paths)
fetch.results(fs)  # generates results in order
fetch.results(fs, timeout=None)  # generates results as completed

fetch.map(urls)  # generates results in order
fetch.map(urls, timeout=None)  # generates results as completed

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

@threaded
def slow():
   ...

fetch = threaded(requests.Session().get, url)
fetch(params=...)

Methods are supported.

class FutureSession(requests.Session):
   request = threaded(requests.Session.request)

As well as a decorated utility for automatically subclassing.

from futured import decorated

FutureSession = decorated(requests.Session, request=threaded)

Thread and process pool executors can also be customized and reused.

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

The same interface works for aynscio. For convenience, there’s also a synchronous run method.

from futured import asynced
import aiohttp

fetch = asynced(aiohttp.ClientSession().get)
fetch.map(urls, timeout=...)  # generates results as described above
fetch.run(url)  # single synchronous call

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

from futured import futured, command

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

futured(command, 'ls')

forked allows iteration in separate child processes.

from futured import forked

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

Installation

$ pip install futured

Dependencies

  • Python 3.5+

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-0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

futured-0.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: futured-0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for futured-0.1.tar.gz
Algorithm Hash digest
SHA256 d8da11cfb3ce48154722bd6c6d42fa67637ccf3b801b801789eb17e161cd43d7
MD5 385aa16a689f0edc30a1f0486a105c42
BLAKE2b-256 5dec341c9f89f49ae8cfb16bb08953a0df37f82d911414b1187af8abf70f318b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for futured-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 889e8033defff6f41be9b73bda55a46c9a5128204912764b9579dc0404cac12e
MD5 da7db36802a91c8388a2a7e437f7ae82
BLAKE2b-256 01523e2e4f64881e3b3bda051e47bfadfdd53c0a8587fc22a94684a0474a2eda

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