Skip to main content

Thread based async library for python

Project description

Fast Async

Publish to PyPi

A thread based, asynchronous programming framework built for Python. Designed and optimized for speed.

Asyncio, the go-to asynchronous programming framework for Python, uses a single-threaded event loop to achieve concurrency. Although this prevents unnecessary computational overheads and race conditions, it is inherently not as fast as threads (even with inefficiencies brought along with GIL). In some scenarios where speed is of utmost importance and where computational resources are abundant, then it makes sense to use a multi-threading approach to concurrency.

Fast Async is a high-level API for Python threads, providing users with the ability to await asynchronous code, and other features such as event-driven, pubsub model (similar to Javascript's Promise.then()). It aims to serve as an alternative to asyncio, for users who require faster execution speed.

Installation

Run pip install fast-async

Running locally

Clone the repository and make the working directory src/.

Alternatively, extract the folder src/fast_async.

Benchmarks

Scenario (sample.py)

A long-running network request and an expensive operation is executed asynchronously

Result

fast-async is, on average, almost 50% faster than asyncio due to asyncio executing the two tasks almost sequentially whilst fast-async leverages threads to execute them in parallel.

FAQ

When to use fast-async

Fast-async should be used when execution speed is a higher priority. For example, uploading each frame of a video stream to a remote server. For cases where execution speed is not important, or when well-written code make the speed differences negligible, asyncio is preferred.

What about ThreadPoolExecutor?

ThreadPoolExecutor is a Python built-in class that offers some of the same functionalities as fast-async, namely the ability to wait for tasks, and limiting threads to conserve resources. However, fast-async is more feature-rich, such as the event-driven model (subscribers and callbacks) and various utility functions that mirror certain useful functionalities from other languages (such as JavaScript). Fast-async is designed to enhance developer experience when working with threads, by offering an easy-to-use interface and minimal pre-requisite knowledge.

Documentation

Decorators

@make_async

Make a function asynchronous. Functions that are decorated with make_async will return an object of type AsyncTask

Aside from its type, decorated functions can be treated as a normal function. This means arguments can be passed in, much like a regular function.

Exceptions raised within the decorated function will be caught and re-thrown in the caller thread.

Example:

from fast_async import make_async

@make_async
def hello(message):
    print("hello world")
    return message

# Awaits hello to finish executing
return_val = hello("hello world").wait()

# Prints "hello world"
print(return_val)

Classes

Package: fast_async.types.tasks

class AsyncTask(func: Callable, *args, **kwargs)

Attributes

  • func: A function or Callable.
  • *args: Non-keyworded arguments for func
  • **kwargs: Keyworded arguments for func
  • status: Current status of func (pending, success, failure)
  • result: Return value of func
  • thread: Thread that func is being ran on
  • exception: First caught Exception raised in func

Methods

run()

Runs func on a child thread, returns None.

wait()

Awaits func to finish executing (blocks the caller thread), returns the return value of func.

subscribe(on_success: Callable, on_failure: Callable, blocks: bool = False)

Subscribes success and failure callbacks that is invoked when task is finished executing or raised an exception. Optional blocks argument controls whether subscribe blocks the caller thread (by default subscribe does not block)

Functions

set_max_threads(num: int): None

Set the max number of threads available to be consumed by tasks. Default is 64 threads. Useful when wanting to dynamically scale usage.

Example:

from fast_async import set_max_threads

set_max_threads(3) # Only allows a maximum of 3 concurrent threads

await_all(tasks: List[AsyncTask]): List

Waits for all tasks in the tasks list to finish executing, or when a task fails, then the function will immediately raise an exception and exit.

Returns a list of results corresponding to the list of tasks provided.

Similar to JavaScript's Promise.all()

Example:

from fast_async import make_async
from fast_async.utils import await_all

@make_async
def func1():
    return 1

@make_async
def func2():
    return 2

await_all([func1(), func2()]) # Will return [1, 2]

await_first(tasks: List[AsyncTask]): Any

Waits for the first task in tasks list to finish executing and immediately returns the result. If all tasks fail, then the first failed task is raised in an exception.

Returns the result of the first successful task.

Similar to JavaScript's Promise.race()

Example

from fast_async import make_async
from fast_async.utils import await_first
import time

@make_async
def func1():
    time.sleep(1)
    return 1

@make_async
def func2():
    time.sleep(2)
    return 2

await_first([func1(), func2()]) # Will return 1, because func1 finishes first

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

fast-async-1.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

fast_async-1.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file fast-async-1.1.0.tar.gz.

File metadata

  • Download URL: fast-async-1.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for fast-async-1.1.0.tar.gz
Algorithm Hash digest
SHA256 2fd90aca302a34377fd6687f8b2fba50a48563414957df1f7ec1915a00ad48b5
MD5 7ec1e22e6183c69307ed7b07dde5870a
BLAKE2b-256 54ed05b08eb6952b802270e910ba9949921b14ef3537d3eee345ed950a8f9c55

See more details on using hashes here.

File details

Details for the file fast_async-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: fast_async-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for fast_async-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bd2bf82218c42f2e7eb7d72e04c1809f17b96de674a50d24d0758af43a9256fe
MD5 6021dd9a059683302df5e4110f3b533b
BLAKE2b-256 0ceb74cc2f393c5d4bad3ab30838474741808ca8bbb0c3117352c6070f30f4ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page