[](https://opensource.org/licenses/Apache-2.0) 
# 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.
```bash
pip install async_obj
```
## Examples
- Run a function asynchronous and check for completion. Then collect the result.
```python
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.
```python
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()`.
```python
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.
```python
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "async-obj",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "asynchronous, async, asyncio, concurrency, concurrent, thread, threading, parallelization, non-blocking, wrapper, function, class",
"author": "Gun Deniz Akkoc",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/ac/9c/e21941834231422dac51e8dc703213e21b44519fad0128b07675c1b2f19b/async_obj-1.0.2.tar.gz",
"platform": null,
"description": "[](https://opensource.org/licenses/Apache-2.0) \r\n\r\n# async_obj\r\n\r\nA minimalist and lightweight Python wrapper to make any function -standalone or within an object- asynchronous.\r\n\r\nIf 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.\r\n\r\n`async obj` enables:\r\n- Simply running a function in a dedicated thread.\r\n- Check for completion of the function.\r\n- Block until the function finishes.\r\n- Receiving the returned result.\r\n- In case of an exception, raising the exception on demand (whenever the result is requested).\r\n\r\nAdvantages:\r\n- No compatibility concerns (e.g., `asyncio`), only depends on a few standard Python libraries.\r\n- Essentially a 2 liner, no decorators or keywords.\r\n- Mimics an object or a function and does not create a copy, hence minimum impact on memory and performance.\r\n\r\n## Installation\r\nEasiest way is to use `pip`, only requirement is Python >=3.8.\r\n\r\n```bash\r\npip install async_obj\r\n```\r\n\r\n## Examples\r\n\r\n- Run a function asynchronous and check for completion. Then collect the result.\r\n```python\r\nfrom async_obj import async_obj\r\nfrom time import sleep\r\n\r\ndef dummy_func(x:int):\r\n sleep(3)\r\n return x * x\r\n\r\n#define the async version of the dummy function\r\nasync_dummy = async_obj(dummy_func)\r\n\r\nprint(\"Starting async function...\")\r\nasync_dummy(2) # Run dummy_func asynchronously\r\nprint(\"Started.\")\r\n\r\nwhile True:\r\n print(\"Checking whether the async function is done...\")\r\n if async_dummy.async_obj_is_done():\r\n print(\"Async function is done!\")\r\n print(\"Result: \", async_dummy.async_obj_get_result(), \" Expected Result: 4\")\r\n break\r\n else:\r\n print(\"Async function is still running...\")\r\n sleep(1)\r\n```\r\n\r\n---\r\n- Alternatively, block until the function is completed, also retrieve any results.\r\n\r\n```python\r\nprint(\"Starting async function...\")\r\nasync_dummy(4) # Run dummy_func asynchronously\r\nprint(\"Started.\")\r\nprint(\"Blocking until the function finishes...\")\r\nresult = async_dummy.async_obj_wait()\r\nprint(\"Function finished.\")\r\nprint(\"Result: \", result, \" Expected Result: 16\")\r\n```\r\n\r\n---\r\n- Raise propagated exceptions, whenever the result is requested either with `async_obj_get_result()` or with `async_obj_wait()`.\r\n\r\n```python\r\nprint(\"Starting async function with an exception being expected...\")\r\nasync_dummy(None) # pass an invalid argument to raise an exception\r\nprint(\"Started.\")\r\nprint(\"Blocking until the function finishes...\")\r\ntry:\r\n result = async_dummy.async_obj_wait()\r\nexcept Exception as e:\r\n print(\"Function finished with an exception: \", str(e))\r\nelse:\r\n print(\"Function finished without an exception, which is unexpected.\")\r\n```\r\n---\r\n- Same functionalities are available for functions within class instances.\r\n```python\r\nclass dummy_class:\r\n x = None\r\n\r\n def __init__(self):\r\n self.x = 5\r\n\r\n def dummy_func(self, y:int):\r\n sleep(3)\r\n return self.x * y\r\n\r\ndummy_instance = dummy_class()\r\n#define the async version of the dummy function within the dummy class instance\r\nasync_dummy = async_obj(dummy_instance)\r\n\r\nprint(\"Starting async function...\")\r\nasync_dummy.dummy_func(4) # Run dummy_func asynchronously\r\nprint(\"Started.\")\r\nprint(\"Blocking until the function finishes...\")\r\nresult = async_dummy.async_obj_wait()\r\nprint(\"Function finished.\")\r\nprint(\"Result: \", result, \" Expected Result: 20\")\r\n```\r\n\r\n## To Do\r\n1. Providing same concurrency functionalities for `getter`, `setter` and similar extension.\r\n2. Improve autocompletion support for better IDE convenience.\r\n",
"bugtrack_url": null,
"license": null,
"summary": "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.",
"version": "1.0.2",
"project_urls": {
"Issues": "https://github.com/gunakkoc/async_obj/issues",
"Repository": "https://github.com/gunakkoc/async_obj"
},
"split_keywords": [
"asynchronous",
" async",
" asyncio",
" concurrency",
" concurrent",
" thread",
" threading",
" parallelization",
" non-blocking",
" wrapper",
" function",
" class"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "5b328dc6b2b121dcfda371215cf5244034301fed036c4504e92371ec92eb6ed4",
"md5": "4fa57534fdc14656bd3ba8b7d60ac808",
"sha256": "e32941027ff939e8c0d11106f745251da5dd9b207033cba39d098c538142cc56"
},
"downloads": -1,
"filename": "async_obj-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4fa57534fdc14656bd3ba8b7d60ac808",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 9408,
"upload_time": "2025-07-23T22:09:42",
"upload_time_iso_8601": "2025-07-23T22:09:42.228077Z",
"url": "https://files.pythonhosted.org/packages/5b/32/8dc6b2b121dcfda371215cf5244034301fed036c4504e92371ec92eb6ed4/async_obj-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "ac9ce21941834231422dac51e8dc703213e21b44519fad0128b07675c1b2f19b",
"md5": "7d277ba477209a7a8a8362256ed31dec",
"sha256": "4c04e89c910a32d7f6ba6eb071205d437a05261bdd672f81f72509615be1cbf6"
},
"downloads": -1,
"filename": "async_obj-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "7d277ba477209a7a8a8362256ed31dec",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 9108,
"upload_time": "2025-07-23T22:09:43",
"upload_time_iso_8601": "2025-07-23T22:09:43.222129Z",
"url": "https://files.pythonhosted.org/packages/ac/9c/e21941834231422dac51e8dc703213e21b44519fad0128b07675c1b2f19b/async_obj-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-23 22:09:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gunakkoc",
"github_project": "async_obj",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "async-obj"
}