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

Manage asyncio event loops explicitly #998

Merged
merged 3 commits into from
Feb 6, 2023
Merged

Manage asyncio event loops explicitly #998

merged 3 commits into from
Feb 6, 2023

Conversation

nolar
Copy link
Owner

@nolar nolar commented Feb 6, 2023

This should address the failing CI tests with uvloop, but also to make the event loop more straightforward in general, especially in Python 3.11.1:

In Python 3.11.1, a warning was added if policy.get_event_loop() is called without previously calling the policy.set_event_loop(), i.e. when a new loop is implicitly created.

Instead, the loops must be created and closed explicitly in the code. Alternatively, asyncio.run() must be used for managed loops. However, we still have to accept an external loop or default loop for embedded operators — assuming those loops are managed elsewhere.


As for the debug mode, it was needed at the earlier stages of the development but is not needed anymore. However, it brings much inconvenience too:

The event loop must be known already at the stage of logging configuration, while it is normally created much later in the kopf.run(). The get_event_loop(), in turn, emits a warning about a deprecated use with no event loop set in advance, which is converted to errors in tests.

Trying to fix it leads to overly complicated solutions, which look like overkill for the unneeded debug mode. It is easier to remove it.

@nolar nolar added the refactoring Code cleanup without new features added label Feb 6, 2023
The debug mode was needed at the earlier stages of the development but is not needed anymore. However, it brings much inconvenience:

The event loop must be known already at the stage of logging configuration, while it is normally created much later in the `kopf.run()`. The `get_event_loop()`, in turn, emits a warning about a deprecated use with no event loop set in advance, which is converted to errors in tests. It seems, the event loop management is supposed to be explicit — so be it.

However, trying to `set_event_loop()` up in the stack if CLI commands brings other issues and warnings: there is no place to close that newly created loop unless decorators with context managers are added for all CLI commands. This seems an overkill for the unneeded debug mode. There is the "proper" way to run an implicit loop — `asyncio.run()` — so let's use it.

Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Code cleanup without new features added
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant