From 075fdc5a4ed1752e3bc4c6b61de87301c85aea4f Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Sun, 30 Apr 2023 20:49:07 +0100 Subject: [PATCH] Sync in asyncio (#42) --- .github/workflows/test.yaml | 2 +- kr8s/_asyncio.py | 4 ++++ kr8s/tests/test_api.py | 1 - poetry.lock | 14 +++++++++++++- pyproject.toml | 1 + 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 245c071d..ad4e3de4 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -42,7 +42,7 @@ jobs: uses: actions/cache@v2 with: path: .venv - key: pydeps-${{ hashFiles('**/poetry.lock') }} + key: pydeps-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }} - run: poetry install --no-interaction --no-root if: steps.cache-deps.outputs.cache-hit != 'true' - run: poetry install --no-interaction diff --git a/kr8s/_asyncio.py b/kr8s/_asyncio.py index 57634f63..7d1e41af 100644 --- a/kr8s/_asyncio.py +++ b/kr8s/_asyncio.py @@ -7,6 +7,10 @@ import inspect from typing import Any, AsyncGenerator, Callable, Generator, Tuple +import nest_asyncio + +nest_asyncio.apply() + def _get_event_loop() -> asyncio.AbstractEventLoop: try: diff --git a/kr8s/tests/test_api.py b/kr8s/tests/test_api.py index c32dab90..1a789eec 100644 --- a/kr8s/tests/test_api.py +++ b/kr8s/tests/test_api.py @@ -57,7 +57,6 @@ def test_version_sync(): assert "major" in version -@pytest.mark.xfail(reason="Cannot run nested event loops", raises=RuntimeError) async def test_version_sync_in_async(): kubernetes = kr8s.api() version = kubernetes.version() diff --git a/poetry.lock b/poetry.lock index 2d7d839f..a7c11c0d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -905,6 +905,18 @@ rtd = ["ipython", "pydata-sphinx-theme (==v0.13.0rc4)", "sphinx-autodoc2 (>=0.4. testing = ["beautifulsoup4", "coverage[toml]", "pytest (>=7,<8)", "pytest-cov", "pytest-param-files (>=0.3.4,<0.4.0)", "pytest-regressions", "sphinx-pytest"] testing-docutils = ["pygments", "pytest (>=7,<8)", "pytest-param-files (>=0.3.4,<0.4.0)"] +[[package]] +name = "nest-asyncio" +version = "1.5.6" +description = "Patch asyncio to allow nested event loops" +category = "main" +optional = false +python-versions = ">=3.5" +files = [ + {file = "nest_asyncio-1.5.6-py3-none-any.whl", hash = "sha256:b9a953fb40dceaa587d109609098db21900182b16440652454a146cffb06e8b8"}, + {file = "nest_asyncio-1.5.6.tar.gz", hash = "sha256:d267cc1ff794403f7df692964d1d2a3fa9418ffea2a3f6859a439ff482fef290"}, +] + [[package]] name = "packaging" version = "23.1" @@ -1714,4 +1726,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "17da29eb656a2129ac5b97c9afa01db6dae0341bb6cfb00a924eb194ce25c70c" +content-hash = "bb8e1dd36d3b060bcd412571a976efc1c9adb41d48dc2937460d2f093989623a" diff --git a/pyproject.toml b/pyproject.toml index 2d39160f..e6bc9b21 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ python = "^3.8" aiohttp = "^3.8.4" pyyaml = "^6.0" asyncio-atexit = "^1.0.1" +nest-asyncio = "^1.5.6" [tool.poetry.group.test.dependencies]