Skip to main content

F-Race, bunch of python awaitables racing to get fastest response considering exceptions, retries and backoffs

Project description

Frace - Python Functions in a Race

Welcome to the Frace Project, a powerful Python library designed to execute multiple functions concurrently. Frace ensures rapid and reliable execution by prioritizing latency and aiming for quick success across functions intended to accomplish the same goal. Even if some functions encounter failures, others continue to run, offering results as soon as possible.

Key Features

  • Resilient Execution: Automatically transitions to alternate functions upon failures, ensuring the best chance of success.
  • Concurrency: Executes one function from each bucket concurrently, enhancing overall performance.
  • Customizable Timeouts: Allows specific timeouts for individual functions to manage long-running tasks.
  • Failure Management: Monitors function performance, optimizing subsequent executions by marking functions that fail.
  • Exponential Backoff: Employs exponential backoff for failed functions to prevent system overloads and disables functions that fail repeatedly.

Why Choose Frace?

When dealing with operations that may experience intermittent failures and where getting a result quickly is more important than which function succeeds, Frace is ideal. Its concurrent execution and automatic failover to alternate functions ensure your application remains responsive and reliable.

Frace at a Glance

Frace executes one function from each bucket concurrently, allowing multiple functions to run simultaneously, If a function fails, Frace automatically switches to the next available function in the bucket. This process continues until a successful function is found or all functions in the bucket fail.. while execution if any bucket gives a successful response, the execution stops and returns the result.

Installation

Install the frace package using pip:

$ pip install frace

Or install the latest package directly from GitHub:

$ pip install git+https://github.com/searchX/frace

Handling Failures

Frace automatically handles function failures by attempting to execute the next available function within the defined bucket. If all functions in a bucket fail, a FraceException is raised.

Use Case: Large Language Model Completions

Suppose you need to get a response from large language models and multiple models are available. It doesn't matter which model provides the output as long as it returns quickly. You can use Frace to manage this redundancy efficiently by running calls to different models concurrently, taking the first successful response.

import asyncio
import pytest
from frace import FunctionModel

def get_fast_llm_response():
    race_caller = FunctionRaceCaller(max_failures=2, function_timeouts={})

    async def llm_model_a():
        await asyncio.sleep(1)  # Simulate delay for Model A
        return "Model A Response"

    async def llm_model_b():
        await asyncio.sleep(0.5)  # Simulate faster delay for Model B
        return "Model B Response"

    # Register the functions as models
    model_a = FunctionModel(id="model_a", func=llm_model_a)
    model_b = FunctionModel(id="model_b", func=llm_model_b)

    race_caller.register_function(model_a)
    race_caller.register_function(model_b)

    # Use buckets to create a group of functions to race
    buckets = [["model_a", "model_b"]]

    # Start the event loop to run the asynchronous function
    result = asyncio.run(race_caller.call_functions(buckets))
    assert result == "Model B Response"  # Expect the fastest function result

Contributing

Frace is an essential tool for achieving resilience in concurrent function execution. Contributions are welcome to extend its capabilities and improve its robustness. Feel free to submit issues or pull requests on GitHub.

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

frace-0.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

frace-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file frace-0.1.0.tar.gz.

File metadata

  • Download URL: frace-0.1.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.2 CPython/3.13.1 Linux/6.12.8-200.fc41.x86_64

File hashes

Hashes for frace-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eec2015702d97c6cf7e375669a772f942c6c689645c7e122cb22a1976f0a1f26
MD5 d85c9795269d329c7fcd9199a96e9564
BLAKE2b-256 b567bfe55d7a2e453b52c28d1a2873aed7708a9da96f3a0fbb5ab1d0fec88dfb

See more details on using hashes here.

File details

Details for the file frace-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: frace-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.2 CPython/3.13.1 Linux/6.12.8-200.fc41.x86_64

File hashes

Hashes for frace-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b4aa7f8ebd6646d34c205a8aad62ad02c07467cde85e601d46ee9a85c3389b4
MD5 ceb44776dfe22acefc2dc4f1b9f4029e
BLAKE2b-256 62b790f1bc838b1dfc33e4bcb90e3dccc157d045ba17dc92005be2ac862a2350

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