{"id":13468831,"url":"https://github.com/simonw/asyncinject","last_synced_at":"2025-10-06T02:38:07.793Z","repository":{"id":42008977,"uuid":"428871319","full_name":"simonw/asyncinject","owner":"simonw","description":"Run async workflows using pytest-fixtures-style dependency injection","archived":false,"fork":false,"pushed_at":"2023-04-14T01:28:28.000Z","size":38,"stargazers_count":34,"open_issues_count":3,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-09-25T16:30:17.795Z","etag":null,"topics":["asyncio"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simonw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-11-17T01:35:15.000Z","updated_at":"2024-08-13T03:35:30.000Z","dependencies_parsed_at":"2024-01-18T20:05:26.733Z","dependency_job_id":null,"html_url":"https://github.com/simonw/asyncinject","commit_stats":{"total_commits":32,"total_committers":2,"mean_commits":16.0,"dds":0.03125,"last_synced_commit":"ddb71fdbf996fd82a3ea710e30718e021da568da"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/simonw/asyncinject","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasyncinject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasyncinject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasyncinject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasyncinject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonw","download_url":"https://codeload.github.com/simonw/asyncinject/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasyncinject/sbom","scorecard":{"id":825145,"data":{"date":"2025-08-18","repo":{"name":"github.com/simonw/asyncinject","commit":"4a74e803ffc90e00bef7d73297d03efd1cccb06c"},"scorecard":{"version":"v5.2.1-41-g40576783","commit":"40576783fda6698350fcbbeaea760ff827433034"},"score":3.4,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 1/28 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#dangerous-workflow"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/simonw/asyncinject/publish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/simonw/asyncinject/publish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/simonw/asyncinject/publish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/simonw/asyncinject/publish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/simonw/asyncinject/test.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/simonw/asyncinject/test.yml/main?enable=pin","Warn: pipCommand not pinned by hash: .github/workflows/publish.yml:23","Warn: pipCommand not pinned by hash: .github/workflows/publish.yml:39","Warn: pipCommand not pinned by hash: .github/workflows/test.yml:21","Info:   0 out of   6 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   3 pipCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#pinned-dependencies"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/publish.yml:1","Warn: no topLevel permission defined: .github/workflows/test.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#cii-best-practices"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#branch-protection"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#security-policy"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 5 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/40576783fda6698350fcbbeaea760ff827433034/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-23T16:28:58.232Z","repository_id":42008977,"created_at":"2025-08-23T16:28:58.232Z","updated_at":"2025-08-23T16:28:58.232Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278551080,"owners_count":26005385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio"],"created_at":"2024-07-31T15:01:19.968Z","updated_at":"2025-10-06T02:38:07.759Z","avatar_url":"https://github.com/simonw.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# asyncinject\n\n[![PyPI](https://img.shields.io/pypi/v/asyncinject.svg)](https://pypi.org/project/asyncinject/)\n[![Changelog](https://img.shields.io/github/v/release/simonw/asyncinject?include_prereleases\u0026label=changelog)](https://github.com/simonw/asyncinject/releases)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/asyncinject/blob/main/LICENSE)\n\nRun async workflows using pytest-fixtures-style dependency injection\n\n## Installation\n\nInstall this library using `pip`:\n\n    $ pip install asyncinject\n\n## Usage\n\nThis library is inspired by [pytest fixtures](https://docs.pytest.org/en/6.2.x/fixture.html).\n\nThe 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.\n\nThe library can then create and execute a plan for executing the required functions in parallel in the most efficient sequence possible.\n\nHere's an example, using the [httpx](https://www.python-httpx.org/) HTTP library.\n\n```python\nfrom asyncinject import Registry\nimport httpx\n\n\nasync def get(url):\n    async with httpx.AsyncClient() as client:\n        return (await client.get(url)).text\n\nasync def example():\n    return await get(\"http://www.example.com/\")\n\nasync def simonwillison():\n    return await get(\"https://simonwillison.net/search/?tag=empty\")\n\nasync def both(example, simonwillison):\n    return example + \"\\n\\n\" + simonwillison\n\nregistry = Registry(example, simonwillison, both)\ncombined = await registry.resolve(both)\nprint(combined)\n```\nIf you run this in `ipython` or `python -m asyncio` (to enable top-level await in the console) you will see output that combines HTML from both of those pages.\n\nThe HTTP requests to `www.example.com` and `simonwillison.net` will be performed in parallel.\n\nThe 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.\n\n### Registry.from_dict()\n\nPassing a list of functions to the `Registry` constructor will register each function under their introspected function name, using `fn.__name__`.\n\nYou can set explicit names instead using a dictionary:\n\n```python\nregistry = Registry.from_dict({\n    \"example\": example,\n    \"simonwillison\": simonwillison,\n    \"both\": both\n})\n```\nThose string names will be used to match parameters, so each function will need to accept parameters named after the keys used in that dictionary.\n\n### Registering additional functions\n\nFunctions that are registered can be regular functions or `async def` functions.\n\nIn addition to registering functions by passing them to the constructor, you can also add them to a registry using the `.register()` method:\n\n```python\nasync def another():\n    return \"another\"\n\nregistry.register(another)\n```\nTo register them with a name other than the name of the function, pass the `name=` argument:\n```python\nasync def another():\n    return \"another 2\"\n\nregistry.register(another, name=\"another_2\")\n```\n\n### Resolving an unregistered function\n\nYou don't need to register the final function that you pass to `.resolve()` - if you pass an unregistered function, the library will introspect the function's parameters and resolve them directly.\n\nThis works with both regular and async functions:\n\n```python\nasync def one():\n    return 1\n\nasync def two():\n    return 2\n\nregistry = Registry(one, two)\n\n# async def works here too:\ndef three(one, two):\n    return one + two\n\nprint(await registry.resolve(three))\n# Prints 3\n```\n\n### Parameters are passed through\n\nYour dependent functions can require keyword arguments which have been passed to the `.resolve()` call:\n\n```python\nasync def get_param_1(param1):\n    return await get(param1)\n\nasync def get_param_2(param2):\n    return await get(param2)\n\nasync def both(get_param_1, get_param_2):\n    return get_param_1 + \"\\n\\n\" + get_param_2\n\n\ncombined = await Registry(get_param_1, get_param_2, both).resolve(\n    both,\n    param1 = \"http://www.example.com/\",\n    param2 = \"https://simonwillison.net/search/?tag=empty\"\n)\nprint(combined)\n```\n### Parameters with default values are ignored\n\nYou can opt a parameter out of the dependency injection mechanism by assigning it a default value:\n\n```python\nasync def go(calc1, x=5):\n    return calc1 + x\n\nasync def calc1():\n    return 5\n\nprint(await Registry(calc1, go).resolve(go))\n# Prints 10\n```\n\n### Tracking with a timer\n\nYou can pass a `timer=` callable to the `Registry` constructor to gather timing information about executed tasks..  Your function should take three positional arguments:\n\n- `name` - the name of the function that is being timed\n- `start` - the time that it started executing, using `time.perf_counter()` ([perf_counter() docs](https://docs.python.org/3/library/time.html#time.perf_counter))\n- `end` - the time that it finished executing\n\nYou can use `print` here too:\n\n```python\ncombined = await Registry(\n    get_param_1, get_param_2, both, timer=print\n).resolve(\n    both,\n    param1 = \"http://www.example.com/\",\n    param2 = \"https://simonwillison.net/search/?tag=empty\"\n)\n```\nThis will output:\n```\nget_param_1 436633.584580685 436633.797921747\nget_param_2 436633.641832699 436634.196364347\nboth 436634.196570217 436634.196575639\n```\n### Turning off parallel execution\n\nBy default, functions that can run in parallel according to the execution plan will run in parallel using `asyncio.gather()`.\n\nYou 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.\n\nThis is mainly useful for benchmarking the difference between parallel and serial execution for your project.\n\n## Development\n\nTo contribute to this library, first checkout the code. Then create a new virtual environment:\n\n    cd asyncinject\n    python -m venv venv\n    source venv/bin/activate\n\nNow install the dependencies and test dependencies:\n\n    pip install -e '.[test]'\n\nTo run the tests:\n\n    pytest\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonw%2Fasyncinject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonw%2Fasyncinject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonw%2Fasyncinject/lists"}