Fix qasync.run in Python 3.11 #83
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
QEventLoop
s previously set themselves as the running loop by default on construction, regardless of if they are actually running or not. Foralready_running
loops, this is fine, but for non-already_running
loops, this setting of the running loop is premature.This is because
asyncio
may check for an existing running loop after it constructs a newQEventLoop
, but before running it. If it does, it will falsely detect the newly constructed, never ran loop as the "running" loop.This didn't happen until Python 3.11, where
asyncio.run
checks for a running loop both before and after getting a newQEventLoop
with the use of the newasyncio.Runner.run
, hence raising aRuntimeError
.Set
QEventLoop
s as the running loop on construction exclusively when they are already running. This means the set_running_loop constructor parameter is now unused, but for backwards compatibility I've left it intact (as existing code already referred to this parameter by keyword).The original commit that put it there was, from what I can tell, in response to a
RuntimeError
raised when callingasyncio.sleep
because of Python 3.8 switching to using get_running_loop in asyncio.sleep in place of get_event_loop. For non-already_running
loops, since the fix in #75, this is redundant, as QEventLoop.run_forever sets the running loop.Fixes #68.