# aioexec
## Description
Aioexec is a simple, intuitive interface around the `concurrent.futures` package and asyncio's `loop.run_in_executor` method. Aioexec is leightweight, no dependencies and ~100 LOC.
## Requirements
aioexec requires Python `>= 3.7`
## Install
pip install aioexec
or
pipenv install aioexec
## Usage
**Without** `aioexec` you usually run an executor something like this:
```python
import aysncio
from concurrent.futures import ProcessPoolExecutor
# ...
loop = asyncio.get_event_loop()
foo = await loop.run_in_executor(
ProcessPoolExecutor(1), lambda: my_func(foo='baz')
)
```
**With** `aioexec` you would do the same like this:
```python
from aioexec import Procs
# ...
foo = await Procs(1).call(my_func, foo='baz')
```
You can pass both `sync` and `async` functions to an executor:
```python
def my_sync_func(foo):
return stuff(foo)
async def my_async_func(foo):
return await stuff(foo)
foo = await Procs(1).call(my_sync_func, foo='baz')
foo = await Procs(1).call(my_async_func, foo='baz')
```
You can call a `batch` of functions in the same executor like this:
```python
import asyncio
from aioexec import Procs, Call
# ...
my_values = await asyncio.gather(
*Procs(3).batch(
Call(my_func, foo='bar'),
Call(my_func, foo='baz'),
Call(my_func, foo='qux'),
)
)
```
This plays nicely with comprehensions:
```python
my_values = await asyncio.gather(
*Procs(10).batch(
Call(my_func, foo=i) for i in range(0, 10)
)
)
```
You can also spawn a `pool` in a context and make multiple different calls with the same executor:
```python
with Procs(10) as pool:
value_a = await pool.call(my_func, foo='baz')
value_b = await aio.gather(
*pool.batch(
Call(my_func, foo=i) for i in range(0, 10)
)
)
# etc...
```
The examples from above work the same for `Threads`, e.g.:
```python
from aioexec import Threads
# ...
foo = await Threads(1).call(my_func, foo='baz')
```
If necessary, you can pass an event `loop` to the executors like this:
```python
foo = await Threads(1, my_loop).call(my_func, foo='baz')
foo = await Procs(1, my_loop).call(my_func, foo='baz')
```
## Development / Testing
Clone the repo and install dev packages:
pipenv install --dev
Run tests:
pipenv run python make.py test
Raw data
{
"_id": null,
"home_page": "https://github.com/feluxe/sty",
"name": "aioexec",
"maintainer": "feluxe",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "felix@meyerwolters.de",
"keywords": "asyncio,executors",
"author": "feluxe",
"author_email": "felix@meyerwolters.de",
"download_url": "",
"platform": null,
"description": "# aioexec\n\n## Description\n\nAioexec is a simple, intuitive interface around the `concurrent.futures` package and asyncio's `loop.run_in_executor` method. Aioexec is leightweight, no dependencies and ~100 LOC.\n\n## Requirements\n\naioexec requires Python `>= 3.7`\n\n## Install\n\n pip install aioexec\n\nor\n\n pipenv install aioexec\n\n## Usage\n\n**Without** `aioexec` you usually run an executor something like this:\n\n```python\nimport aysncio\nfrom concurrent.futures import ProcessPoolExecutor\n\n# ...\n\nloop = asyncio.get_event_loop()\n\nfoo = await loop.run_in_executor(\n ProcessPoolExecutor(1), lambda: my_func(foo='baz')\n)\n```\n\n**With** `aioexec` you would do the same like this:\n\n```python\nfrom aioexec import Procs\n\n# ...\n\nfoo = await Procs(1).call(my_func, foo='baz')\n```\n\nYou can pass both `sync` and `async` functions to an executor:\n\n```python\n\ndef my_sync_func(foo):\n return stuff(foo)\n\nasync def my_async_func(foo):\n return await stuff(foo)\n\n\nfoo = await Procs(1).call(my_sync_func, foo='baz')\nfoo = await Procs(1).call(my_async_func, foo='baz')\n```\n\nYou can call a `batch` of functions in the same executor like this:\n\n```python\nimport asyncio\nfrom aioexec import Procs, Call\n\n# ...\n\nmy_values = await asyncio.gather(\n *Procs(3).batch(\n Call(my_func, foo='bar'),\n Call(my_func, foo='baz'),\n Call(my_func, foo='qux'),\n )\n)\n```\n\nThis plays nicely with comprehensions:\n\n```python\nmy_values = await asyncio.gather(\n *Procs(10).batch(\n Call(my_func, foo=i) for i in range(0, 10)\n )\n)\n```\n\nYou can also spawn a `pool` in a context and make multiple different calls with the same executor:\n\n```python\nwith Procs(10) as pool:\n\n value_a = await pool.call(my_func, foo='baz')\n\n value_b = await aio.gather(\n *pool.batch(\n Call(my_func, foo=i) for i in range(0, 10)\n )\n )\n\n # etc...\n```\n\nThe examples from above work the same for `Threads`, e.g.:\n\n```python\nfrom aioexec import Threads\n\n# ...\n\nfoo = await Threads(1).call(my_func, foo='baz')\n\n```\n\nIf necessary, you can pass an event `loop` to the executors like this:\n\n```python\nfoo = await Threads(1, my_loop).call(my_func, foo='baz')\nfoo = await Procs(1, my_loop).call(my_func, foo='baz')\n```\n\n## Development / Testing\n\nClone the repo and install dev packages:\n\n pipenv install --dev\n\nRun tests:\n\n pipenv run python make.py test\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "asyncio executors, clean and simple.",
"version": "0.5.1",
"project_urls": {
"Homepage": "https://github.com/feluxe/sty",
"Repository": "https://github.com/feluxe/sty"
},
"split_keywords": [
"asyncio",
"executors"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "22e08fbcbfcb76d73c8c5424236cb8afa13dc80ef18a1854f52d86d208817be1",
"md5": "0c6388b80e02a274a09c938e063636aa",
"sha256": "7ccba5756d357ab1911eb2836b21308369f6dc36f0bdf3c59d1d6c3ae4494f7a"
},
"downloads": -1,
"filename": "aioexec-0.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0c6388b80e02a274a09c938e063636aa",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 7034,
"upload_time": "2023-11-24T01:12:45",
"upload_time_iso_8601": "2023-11-24T01:12:45.304624Z",
"url": "https://files.pythonhosted.org/packages/22/e0/8fbcbfcb76d73c8c5424236cb8afa13dc80ef18a1854f52d86d208817be1/aioexec-0.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-24 01:12:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "feluxe",
"github_project": "sty",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "aioexec"
}