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

Fix deprecation warnings for asyncio.get_event_loop() #52

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mhthies
Copy link
Owner

@mhthies mhthies commented Feb 19, 2022

In future Python versions, asyncio.get_event_loop() will not create a
new event loop as required. As of Python 3.10 it issues a deprecation
warning when it has to.

This commit changes get_event_loop().run_until_complete() to
asyncio.run(), where possible. However, sometimes we need the event loop
to stay alive after running a coroutine, so we need to ensure having an
event loop manually, similar to how asyncio.run() does.

In future Python versions, asyncio.get_event_loop() will not create a
new event loop as required. As of Python 3.10 it issues a deprecation
warning when it has to.

This commit changes get_event_loop().run_until_complete() to
asyncio.run(), where possible. However, sometimes we need the event loop
to stay alive after running a coroutine, so we need to ensure having an
event loop manually, similar to how asyncio.run() does.
@codecov-commenter
Copy link

codecov-commenter commented Feb 19, 2022

Codecov Report

Merging #52 (8068890) into main (679b9c1) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #52      +/-   ##
==========================================
- Coverage   80.32%   80.29%   -0.04%     
==========================================
  Files          36       36              
  Lines        4468     4471       +3     
  Branches      783      783              
==========================================
+ Hits         3589     3590       +1     
- Misses        718      719       +1     
- Partials      161      162       +1     
Impacted Files Coverage Δ
shc/interfaces/_helper.py 76.22% <100.00%> (-0.20%) ⬇️
shc/supervisor.py 93.05% <100.00%> (+0.40%) ⬆️
shc/timer.py 86.80% <0.00%> (-0.38%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 679b9c1...8068890. Read the comment docs.

@mhthies mhthies marked this pull request as draft February 20, 2022 15:24
@mhthies
Copy link
Owner Author

mhthies commented Feb 20, 2022

I'm not sure if this is actually a valid fix for this problem: The Python documentation states, that asyncio.get_running_loop() "can only be called from a coroutine or a callback", i.e. from code, called from the already running event loop. This is violated in the change of the shc.supervisor module introduced by this PR.

I'm not sure what's the correct way for running a single coroutine in the thread's existing (but not running) event loop, without closing the loop afterwards. We need to do this in multiple tests. I'm also not sure if it's intended to create an event loop, instantiate a lot of objects, including asyncio.Events and asyncio.Futures, bound to the event loop, and finally starting the event loop. This is how SHC is currently working, but it might be against the Python asyncio developer's intention …

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants