Run async workflows using pytest-fixtures-style dependency injection
Project description
asyncinject
Run async workflows using pytest-fixtures-style dependency injection
Installation
Install this library using pip
:
$ pip install asyncinject
Usage
This library is inspired by pytest fixtures.
The idea is to simplify executing parallel asyncio
operations by allowing them to be defined using a collection of functions, where the function arguments represent dependent functions that need to be executed first.
The library can then create and execute a plan for executing the required functions in parallel in the most efficient sequence possible.
Here's an example, using the httpx HTTP library.
from asyncinject import Registry
import httpx
async def get(url):
async with httpx.AsyncClient() as client:
return (await client.get(url)).text
async def example():
return await get("http://www.example.com/")
async def simonwillison():
return await get("https://simonwillison.net/search/?tag=empty")
async def both(example, simonwillison):
return example + "\n\n" + simonwillison
registry = Registry(example, simonwillison, both)
combined = await registry.resolve(both)
print(combined)
If you run this in ipython
(which supports top-level await) you will see output that combines HTML from both of those pages.
The HTTP requests to www.example.com
and simonwillison.net
will be performed in parallel.
The library notices that both()
takes two arguments which are the names of other registered async def
functions, and will construct an execution plan that executes those two functions in parallel, then passes their results to the both()
method.
Parameters are passed through
Your dependent functions can require keyword arguments which have been passed to the .resolve()
call:
async def get_param_1(param1):
return await get(param1)
async def get_param_2(param2):
return await get(param2)
async def both(get_param_1, get_param_2):
return get_param_1 + "\n\n" + get_param_2
combined = await Registry(get_param_1, get_param_2, both).resolve(
both,
param1 = "http://www.example.com/",
param2 = "https://simonwillison.net/search/?tag=empty"
)
print(combined)
Parameters with default values are ignored
You can opt a parameter out of the dependency injection mechanism by assigning it a default value:
async def go(calc1, x=5):
return calc1 + x
async def calc1():
return 5
print(await Registry(calc1, go).resolve(go))
# Prints 10
Tracking with a timer
You can pass a timer=
callable to the Registry
constructor to gather timing information about executed tasks.. Your function should take three positional arguments:
name
- the name of the function that is being timedstart
- the time that it started executing, usingtime.perf_counter()
(perf_counter() docs)end
- the time that it finished executing
You can use print
here too:
combined = await Registry(
get_param_1, get_param_2, both, timer=print
).resolve(
both,
param1 = "http://www.example.com/",
param2 = "https://simonwillison.net/search/?tag=empty"
)
This will output:
get_param_1 436633.584580685 436633.797921747
get_param_2 436633.641832699 436634.196364347
both 436634.196570217 436634.196575639
Turning off parallel execution
By default, functions that can run in parallel according to the execution plan will run in parallel using asyncio.gather()
.
You can disable this parallel exection by passing parallel=False
to the Registry
constructor, or by setting registry.parallel = False
after the registry object has been created.
This is mainly useful for benchmarking the difference between parallel and serial execution for your project.
Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd asyncinject
python -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file asyncinject-0.4.tar.gz
.
File metadata
- Download URL: asyncinject-0.4.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6780037210d92501c65d3bfe0910e4eb6d5394ea6d09641f23913473bcccfd35 |
|
MD5 | aac9fab41d4f88a31668aa6e0c40bcbe |
|
BLAKE2b-256 | 5f91b912345dda675eaccf8b9b0cfc73b60bdd6bdb2a513487da36a30dcf169d |
File details
Details for the file asyncinject-0.4-py3-none-any.whl
.
File metadata
- Download URL: asyncinject-0.4-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c7d425c84dce133e9aaba2eff6f1dd6bd6b122a61c8eb7d1027cd063deaf1d3 |
|
MD5 | 8600c1b9d5443086db096dbe39d68681 |
|
BLAKE2b-256 | b28b6e61aa6161d84b1f7b2e482e824082afa02ca5b7d14035ea0c6306bfb701 |