Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sync API in event loop #41

Closed
jacobtomlinson opened this issue Apr 30, 2023 · 3 comments · Fixed by #42 or #47
Closed

Use sync API in event loop #41

jacobtomlinson opened this issue Apr 30, 2023 · 3 comments · Fixed by #42 or #47
Labels
bug Something isn't working kr8s

Comments

@jacobtomlinson
Copy link
Member

Currently the sync API cannot be used in an asyncio event loop. We have an xfailing test which was added in #40 to demonstrate this.

@pytest.mark.xfail(reason="Cannot run nested event loops", raises=RuntimeError)
async def test_version_sync_in_async():
kubernetes = kr8s.api()
version = kubernetes.version()
assert "major" in version

This situation will come up a lot in Jupyter as there is always an event loop running but many users may expect to be able to use the sync API and will not consider asyncio at all.

We need to be able to support the sync API (which uses asyncio.run under the hood) in an event loop.

@jacobtomlinson jacobtomlinson added bug Something isn't working kr8s labels Apr 30, 2023
@jacobtomlinson
Copy link
Member Author

I started a conversation about this on Twitter and the main suggestions were:

@jacobtomlinson
Copy link
Member Author

Tried nest-asyncio but reverted in #44 as it caused more problems than it solved (see #43). This is likely why Jupyter devs recommended against it.

Reopening to track adding this ability back in either via a solution to #43 or using another method like creating a new thread and loop.

@jacobtomlinson
Copy link
Member Author

I ended up going down the same road as Jupyter and if a loop is already running we start a second loop in a new thread and run coroutines there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working kr8s
Projects
None yet
1 participant