Futurist

3.3.0 · active · verified Wed Apr 15

Futurist is a Python library from OpenStack that provides useful additions to `concurrent.futures`, aiming to offer enhanced transparency in asynchronous work execution. It includes features like statistics gathering for executors, an eventlet executor, a synchronous executor, and more. Currently at version 3.3.0, it is actively maintained with a regular release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `futurist.ThreadPoolExecutor` to run tasks concurrently and collect execution statistics. It submits multiple tasks, waits for their completion, retrieves results, and then displays the executor's performance metrics.

import time
from futurist import ThreadPoolExecutor

def my_task(value):
    """A simple task to demonstrate execution and statistics."""
    time.sleep(0.01) # Simulate some work
    return value * 2

if __name__ == "__main__":
    # Initialize ThreadPoolExecutor with a maximum of 2 workers
    with ThreadPoolExecutor(max_workers=2) as executor:
        print("Submitting tasks...")
        # Submit 5 tasks to the executor
        futures = [executor.submit(my_task, i) for i in range(5)]

        # Retrieve results from completed futures
        results = [f.result() for f in futures]
        print(f"Results: {results}")

        # Access and print execution statistics
        stats = executor.statistics
        print(f"Executed tasks: {stats.executed}")
        print(f"Failed tasks: {stats.failures}")
        print(f"Cancelled tasks: {stats.cancelled}")
        print(f"Total runtime: {stats.runtime:.4f}s")

view raw JSON →