Skip to main content

A simple wrapper to run any function asynchronously in a dedicated thread. Supports standalone functions and functions inside classes as well as result collection and exception propagation.

Project description

License PyPI - Version

async_obj

A minimalist and lightweight Python wrapper to make any function -standalone or within an object- asynchronous.

If you are also tired of adding a whole bunch of code for every single case of simple threading, this small wrapper is my remedy. Requires >=Python 3.8.

async obj enables:

  • Simply running a function in a dedicated thread.
  • Check for completion of the function.
  • Block until the function finishes.
  • Receiving the returned result.
  • In case of an exception, raising the exception on demand (whenever the result is requested).

Advantages:

  • No compatibility concerns (e.g., asyncio), only depends on a few standard Python libraries.
  • Essentially a 2 liner, no decorators or keywords.
  • Mimics an object or a function and does not create a copy, hence minimum impact on memory and performance.

Installation

Easiest way is to use pip, only requirement is Python >=3.8.

pip install async_obj

Examples

  • Run a function asynchronous and check for completion. Then collect the result.
from async_obj import async_obj
from time import sleep

def dummy_func(x:int):
    sleep(3)
    return x * x

#define the async version of the dummy function
async_dummy = async_obj(dummy_func)

print("Starting async function...")
async_dummy(2)  # Run dummy_func asynchronously
print("Started.")

while True:
    print("Checking whether the async function is done...")
    if async_dummy.async_obj_is_done():
        print("Async function is done!")
        print("Result: ", async_dummy.async_obj_get_result(), " Expected Result: 4")
        break
    else:
        print("Async function is still running...")
        sleep(1)

  • Alternatively, block until the function is completed, also retrieve any results.
print("Starting async function...")
async_dummy(4)  # Run dummy_func asynchronously
print("Started.")
print("Blocking until the function finishes...")
result = async_dummy.async_obj_wait()
print("Function finished.")
print("Result: ", result, " Expected Result: 16")

  • Raise propagated exceptions, whenever the result is requested either with async_obj_get_result() or with async_obj_wait().
print("Starting async function with an exception being expected...")
async_dummy(None) # pass an invalid argument to raise an exception
print("Started.")
print("Blocking until the function finishes...")
try:
    result = async_dummy.async_obj_wait()
except Exception as e:
    print("Function finished with an exception: ", str(e))
else:
    print("Function finished without an exception, which is unexpected.")

  • Same functionalities are available for functions within class instances.
class dummy_class:
    x = None

    def __init__(self):
        self.x = 5

    def dummy_func(self, y:int):
        sleep(3)
        return self.x * y

dummy_instance = dummy_class()
#define the async version of the dummy function within the dummy class instance
async_dummy = async_obj(dummy_instance)

print("Starting async function...")
async_dummy.dummy_func(4)  # Run dummy_func asynchronously
print("Started.")
print("Blocking until the function finishes...")
result = async_dummy.async_obj_wait()
print("Function finished.")
print("Result: ", result, " Expected Result: 20")

To Do

  1. Providing same concurrency functionalities for getter, setter and similar extension.
  2. Improve autocompletion support for better IDE convenience.

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

async_obj-1.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

async_obj-1.0.1-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file async_obj-1.0.1.tar.gz.

File metadata

  • Download URL: async_obj-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for async_obj-1.0.1.tar.gz
Algorithm Hash digest
SHA256 62af3674cfb8546e55b87c8236e6d609ed8c4d766270d76c69b60bb4a01701d5
MD5 d6628b745acd316774b5d77ec7a754f7
BLAKE2b-256 e33d7289846adc9946da3389986f45675c65fe88917344ba219802fc527c6aa4

See more details on using hashes here.

File details

Details for the file async_obj-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: async_obj-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for async_obj-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d19bfb0e2ebc5f56f7c4123a1b29831b0f97c24db6b9f296c6c1c9a3dd79099
MD5 f577207ef3672ec840e8b068275d552c
BLAKE2b-256 7ae7256566ada9746b209cd752d5c5844407d23398690da26cba78866c59a490

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