Name | asyncgnostic JSON |
Version |
0.1.2
JSON |
| download |
home_page | |
Summary | Python functions agnostic towards being called with await or otherwise. |
upload_time | 2023-06-03 13:18:48 |
maintainer | |
docs_url | None |
author | Harshad Sharma |
requires_python | >=3.9,<4.0 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# asyncgnostic
Python functions agnostic towards being called with await or otherwise.
Uses [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch)
to automatically call asynchronous or synchronous function based on calling context.
## Example:
#### Automatic sync/async dispatch
```python
import asyncio
from asyncgnostic import awaitable
# Define a sync function
def handler() -> str: # type: ignore
return "Running Sync"
# Pair the sync function with an async function
@awaitable(handler)
async def handler() -> str:
return "Running Async"
# Call the function from sync context
def sync_main():
print("sync context:", handler())
# Call the function from async context
async def async_main():
print("async context:", await handler())
# Run the sync and async functions
sync_main()
asyncio.run(async_main())
```
Output:
```console
sync context: Running Sync
async context: Running Async
```
#### Detect context in your function for sync/async dispatch dispatch
```python
import asyncio
from asyncgnostic import awaited
# Define a sync function
def sync_handler() -> str: # type: ignore
return "Running Sync"
# Define an async function
async def async_handler() -> str:
return "Running Async"
# Define a dispatcher
def handler() -> str:
if awaited():
# If called from async context, call async function
return async_handler()
else:
# If called from sync context, call sync function
return sync_handler()
# Call the function from sync context
def sync_main():
print("sync context", handler())
# Call the function from async context
async def async_main():
print("async context:", await handler())
# Run the sync and async functions
sync_main()
asyncio.run(async_main())
```
Output:
```console
sync context: Running Sync
async context: Running Async
```
## Install
```
pip install asyncgnostic
```
## Credits:
Gratefully borrowed improvements from [curio](https://github.com/dabeaz/curio/).
Reference:
- https://mastodon.sharma.io/@harshad/110476942596328864
- https://mastodon.social/@dabeaz/110477080111974062
- https://github.com/dabeaz/curio/blob/master/curio/meta.py
Raw data
{
"_id": null,
"home_page": "",
"name": "asyncgnostic",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Harshad Sharma",
"author_email": "harshad@sharma.io",
"download_url": "https://files.pythonhosted.org/packages/0c/a4/e08092a8c7613e57cd5ad93c0eb4c16849a49c8352bcf27e047b8b22adcf/asyncgnostic-0.1.2.tar.gz",
"platform": null,
"description": "# asyncgnostic\n\nPython functions agnostic towards being called with await or otherwise.\n\nUses [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch)\nto automatically call asynchronous or synchronous function based on calling context.\n\n## Example:\n#### Automatic sync/async dispatch\n\n```python\nimport asyncio\nfrom asyncgnostic import awaitable\n\n\n# Define a sync function\ndef handler() -> str: # type: ignore\n return \"Running Sync\"\n\n\n# Pair the sync function with an async function\n@awaitable(handler)\nasync def handler() -> str:\n return \"Running Async\"\n\n\n# Call the function from sync context\ndef sync_main():\n print(\"sync context:\", handler())\n\n\n# Call the function from async context\nasync def async_main():\n print(\"async context:\", await handler())\n\n\n# Run the sync and async functions\nsync_main()\nasyncio.run(async_main())\n```\n\nOutput:\n\n```console\nsync context: Running Sync\nasync context: Running Async\n```\n\n\n#### Detect context in your function for sync/async dispatch dispatch \n\n```python\nimport asyncio\nfrom asyncgnostic import awaited\n\n\n# Define a sync function\ndef sync_handler() -> str: # type: ignore\n return \"Running Sync\"\n\n\n# Define an async function\nasync def async_handler() -> str:\n return \"Running Async\"\n\n\n# Define a dispatcher\ndef handler() -> str:\n if awaited():\n # If called from async context, call async function\n return async_handler()\n else:\n # If called from sync context, call sync function\n return sync_handler()\n\n\n# Call the function from sync context\ndef sync_main():\n print(\"sync context\", handler())\n\n\n# Call the function from async context\nasync def async_main():\n print(\"async context:\", await handler())\n\n\n# Run the sync and async functions\nsync_main()\nasyncio.run(async_main())\n```\n\nOutput:\n\n```console\nsync context: Running Sync\nasync context: Running Async\n```\n\n\n## Install\n\n```\npip install asyncgnostic\n```\n\n\n## Credits:\n\nGratefully borrowed improvements from [curio](https://github.com/dabeaz/curio/).\n\nReference:\n - https://mastodon.sharma.io/@harshad/110476942596328864\n - https://mastodon.social/@dabeaz/110477080111974062\n - https://github.com/dabeaz/curio/blob/master/curio/meta.py\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python functions agnostic towards being called with await or otherwise.",
"version": "0.1.2",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "572b7ed737e860ed1b297eeb409dd64fa969285004d94a6d2a755175f1fa95cb",
"md5": "d4b2e67058d6209c016c3e59ca3353cc",
"sha256": "74c603240643e0f1519874b559980a6a53fc4decb8ece2802576761a034954ff"
},
"downloads": -1,
"filename": "asyncgnostic-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d4b2e67058d6209c016c3e59ca3353cc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<4.0",
"size": 3594,
"upload_time": "2023-06-03T13:18:47",
"upload_time_iso_8601": "2023-06-03T13:18:47.289124Z",
"url": "https://files.pythonhosted.org/packages/57/2b/7ed737e860ed1b297eeb409dd64fa969285004d94a6d2a755175f1fa95cb/asyncgnostic-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0ca4e08092a8c7613e57cd5ad93c0eb4c16849a49c8352bcf27e047b8b22adcf",
"md5": "be7276aabbd6050c9398857c37e162f6",
"sha256": "bedf500e53e83f078347998fcef811015783e4f18dc1e88284c8d109d13f1949"
},
"downloads": -1,
"filename": "asyncgnostic-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "be7276aabbd6050c9398857c37e162f6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<4.0",
"size": 2965,
"upload_time": "2023-06-03T13:18:48",
"upload_time_iso_8601": "2023-06-03T13:18:48.875393Z",
"url": "https://files.pythonhosted.org/packages/0c/a4/e08092a8c7613e57cd5ad93c0eb4c16849a49c8352bcf27e047b8b22adcf/asyncgnostic-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-03 13:18:48",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "asyncgnostic"
}