{"id":46491949,"url":"https://github.com/chaoses-ib/nest-asyncio2","last_synced_at":"2026-03-06T11:01:24.610Z","repository":{"id":324926553,"uuid":"1099055176","full_name":"Chaoses-Ib/nest-asyncio2","owner":"Chaoses-Ib","description":"Patch asyncio to allow nested event loops","archived":false,"fork":false,"pushed_at":"2025-11-21T08:21:05.000Z","size":92,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-12T04:39:14.255Z","etag":null,"topics":["async","asyncio","event-loop","nested","recursive"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Chaoses-Ib.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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-18T13:57:03.000Z","updated_at":"2026-01-31T19:39:32.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/Chaoses-Ib/nest-asyncio2","commit_stats":null,"previous_names":["chaoses-ib/nest-asyncio2"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Chaoses-Ib/nest-asyncio2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chaoses-Ib%2Fnest-asyncio2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chaoses-Ib%2Fnest-asyncio2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chaoses-Ib%2Fnest-asyncio2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chaoses-Ib%2Fnest-asyncio2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Chaoses-Ib","download_url":"https://codeload.github.com/Chaoses-Ib/nest-asyncio2/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chaoses-Ib%2Fnest-asyncio2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30173345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T07:56:45.623Z","status":"ssl_error","status_checked_at":"2026-03-06T07:55:55.621Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["async","asyncio","event-loop","nested","recursive"],"created_at":"2026-03-06T11:01:19.519Z","updated_at":"2026-03-06T11:01:24.603Z","avatar_url":"https://github.com/Chaoses-Ib.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nest-asyncio2\n[![Build](https://github.com/Chaoses-Ib/nest-asyncio2/actions/workflows/test.yml/badge.svg?branche=master)](https://github.com/Chaoses-Ib/nest-asyncio2/actions)\n[![status](https://img.shields.io/badge/status-stable-green.svg)]()\n[![PyPi](https://img.shields.io/pypi/v/nest-asyncio2.svg)](https://pypi.python.org/pypi/nest-asyncio2)\n[![License](https://img.shields.io/badge/license-BSD-blue.svg)](LICENSE)\n[![Downloads](https://static.pepy.tech/badge/nest-asyncio2/month)](https://pepy.tech/project/nest-asyncio2)\n\n## Introduction\n\nBy design asyncio [does not allow](https://github.com/python/cpython/issues/66435)\nits event loop to be nested. This presents a practical problem:\nWhen in an environment where the event loop is\nalready running it's impossible to run tasks and wait\nfor the result. Trying to do so will give the error\n\"`RuntimeError: This event loop is already running`\".\n\nThe issue pops up in various environments, such as web servers,\nGUI applications and in Jupyter notebooks.\n\nThis module patches asyncio to allow nested use of `asyncio.run` and\n`loop.run_until_complete`.\n\n## Installation\n\n```sh\npip3 install nest-asyncio2\n```\n\nPython 3.5 or higher is required.\n\n## Usage\n\n```python\nimport nest_asyncio2\nnest_asyncio2.apply()\n```\n\nOptionally the specific loop that needs patching can be given\nas argument to `apply`, otherwise the current event loop is used.\nAn event loop can be patched whether it is already running\nor not. Only event loops from asyncio can be patched;\nLoops from other projects, such as uvloop or quamash,\ngenerally can't be patched.\n\n## Examples\n### [aiohttp](https://github.com/aio-libs/aiohttp)\n```py\n# /// script\n# requires-python = \"\u003e=3.5\"\n# dependencies = [\n#     \"aiohttp\",\n#     \"nest-asyncio2\",\n# ]\n# ///\nimport asyncio\nimport nest_asyncio2\nimport aiohttp\n\nnest_asyncio2.apply()\n\nasync def f_async():\n    # Note that ClientSession must be created and used\n    # in the same event loop (under the same asyncio.run())\n    async with aiohttp.ClientSession() as session:\n        async with session.get('http://httpbin.org/get') as resp:\n            print(resp.status)\n            print(await resp.text())\n            assert resp.status == 200\n\n# async to sync\ndef f():\n    asyncio.run(f_async())\n\nasync def main():\n    f()\nasyncio.run(main())\n```\n\n## Known issues\n### Leaked event loop\n\u003e [!TIP]\n\u003e TL;DR: Usually you don't need to worry about this.\n\u003e The biggest side effect is a `ResourceWarning: unclosed event loop` at exit on Python 3.12+ that is hidden by default.\n\nIf there is no existing event loop, patched `asyncio.run()` will create one but not close it afterwards.\nIt will be reused later, so there will be at most one leaked loop.\n\n`asyncio.run()` will always create and close the loop.\nBut `nest_asyncio` (by accident or intentionally) missed it.\nAs changing this behavior will break existing projects (e.g.\n[ComfyScript](https://github.com/Chaoses-Ib/ComfyScript/issues/117),\n[pyvista](https://github.com/pyvista/pyvista/issues/7938)),\n`nest-asyncio2` follows this behavior.\n\nThis will cause a `ResourceWarning: unclosed event loop` at exit on Python 3.12+,\nalthough it is hidden by default.\n(Note that if you call `asyncio.get_event_loop()` on the main thread without setting the loop before,\n`ResourceWarning` is expected on Python 3.12~3.13, not caused by `nest-asyncio2`.)\n\nIf you want to follow `asyncio.run()`'s behavior and get rid of the `ResourceWarning`,\nyou can set `run_close_loop=True` for all `apply()`:\n```py\nnest_asyncio2.apply(run_close_loop=True)\n```\nOr pass `loop_factory` to `asyncio.run()` on Python 3.12+:\n```py\nasyncio.run(..., loop_factory=asyncio.new_event_loop)\n```\n\n`nest-asyncio2` v2 may change `run_close_loop` to be enabled by default.\n\n## Comparison with `nest_asyncio`\n`nest-asyncio2` is a fork of the unmaintained [`nest_asyncio`](https://github.com/erdewit/nest_asyncio), with the following changes:\n- Support setting `run_close_loop` to avoid [leaked event loop](#leaked-event-loop).\n- Python 3.12 support\n  - `loop_factory` parameter support\n\u003c!--\n  - Fix `ResourceWarning: unclosed event loop` at exit\n\n    Note that if you call `asyncio.get_event_loop()` on the main thread without setting the loop before, `ResourceWarning` is expected on Python 3.12~3.13, not caused by `nest-asyncio2`.\n--\u003e\n- Python 3.14 support\n  - Fix broken `asyncio.current_task()` and others\n  - Fix `DeprecationWarning: 'asyncio.get_event_loop_policy' is deprecated and slated for removal in Python 3.16`\n\n- Fix `call_soon()` callbacks not being called during `run()`\n  ([#3](https://github.com/Chaoses-Ib/nest-asyncio2/issues/3)).\n\n- To avoid potential bugs,\n  `apply()` will warn if `asyncio` is already patched\n  by `nest_asyncio` on Python 3.12+.\n\n  You can also set `error_on_mispatched=True` to\n  turn the warning into a `RuntimeError`, regardless of the Python version.\n  See [#1](https://github.com/Chaoses-Ib/nest-asyncio2/issues/1) for details.\n\nThere is no behavior change on Python \u003c 3.12.\n\nAll interfaces are kept as they are. To migrate, you just need to change the package and module name to `nest_asyncio2`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaoses-ib%2Fnest-asyncio2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchaoses-ib%2Fnest-asyncio2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaoses-ib%2Fnest-asyncio2/lists"}