This repository was archived by the owner on Nov 23, 2017. It is now read-only.
Repository navigation
Ensure get_event_loop returns the running loop when called in a coroutine #355
Closed
Closed
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b30bcdc
Modify the interface for AbstractEventLoopPolicy
705a54c
Update BaseEventLoop.run_forever to set and clear the running loop
2e55a60
Ignore event loop policy warnings during the tests
f117c75
Fix SleepTests and TimeoutTests
1c3a166
Revert "Ignore event loop policy warnings during the tests"
6e8eaa2
Remove running loop context and event loop policy warnings
be2c0f1
Revert "Fix SleepTests and TimeoutTests"
c1cb666
Update docstrings for AbstractEventLoopPolicy
8788c72
Remove get/set_default_loop
121cca3
Add disable_get_running_loop method to TestCase
f1674e8
Update set_event_loop docstring
c634f79
Merge both try-finally blocks into one in BaseEventLoop.run_forever
71fcfab
Fix new_event_loop docstring for AbstractEventLoopPolicy
1a60ee8
Add test_set_running_loop to PolicyTests
db6774d
Clean set_running_loop
28c3de3
Add test_get_event_loop_after_set_running_loop to PolicyTests
5b19055
Fix get_event_loop docstring
7513577
Update running context in BaseEventLoop.run_forever
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove running loop context and event loop policy warnings
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -342,11 +342,15 @@ def run_forever(self): | |
| self._set_coroutine_wrapper(self._debug) | ||
| self._thread_id = threading.get_ident() | ||
| try: | ||
| with self._running_context(): | ||
| policy = events.get_event_loop_policy() | ||
| policy.set_running_loop(self) | ||
| try: | ||
| while True: | ||
| self._run_once() | ||
| if self._stopping: | ||
| break | ||
| finally: | ||
| policy.set_running_loop(None) | ||
| finally: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've left a comment somewhere in this PR on how to merge two try-finally blocks into one. Please do that. |
||
| self._stopping = False | ||
| self._thread_id = None | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has to be outside of the
trystatement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do that, this test (not commited yet) would fail:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm beginning to think that this is all one big misunderstanding. I never
meant get_event_loop() to return a loop that's different from the running
loop. If you have multiple loops associated with the same thread you need
to implement a new EventloopPolicy that keeps track of which one is running
(e.g. via an explicit stack, or perhaps an implicit one using a local
variable to save the event loop and restoring from that in a finally
clause).
The only thing I meant to be possible is for get_event_loop() to raise an
exception, under special circumstances (especially unit tests that are
checking that no code accidentally relies on the default loop).
For library code I think there are two cases it should support:
For application code I think it is totally fine to assume that
get_event_loop() returns the current, running loop. An application written
with this assumption never needs to pass a loop to something it calls
(since library code should always default to using get_event_loop()).
Note all the words devoted in the PEP to "context".
In any case I think we should stop coding and start discussing the use case
you are trying to address on the tulip list, to see whether it's real or
whether you are worrying too much (or whether maybe you should just write a
custom EventloopPolicy for your use case).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum
I actually don't have a use case. I simply noticed that asyncio, 3rd party libraries and users tend to pass the running loop explicitely to their coroutines, because "explicit is better than implicit". That, in my opinion, is unecessary and could be simplified.
Sadly, such a simplification conflicts with the PEP and the way asyncio unittesting currently works. I understand that those constraints makes it hard, and maybe impossible to implement. I orginally meant this PR as a proposal and a starting point towards this simplification.
However, this idea didn't seem to get a lot of support and I don't feel like trying to push it forward anymore. Maybe more people will come up with the same rationale later on, and I'll be happy to help at that time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum
But here's the problem: if you have multiple loops associated with the same thread, policy doesn't know which one is currently running.
That's why I like the idea of adding
policy.set_running_loopandpolicy.get_running_loopfunctions, because it makes policies more capable and allows you to worry less about multiple running loops event with the default policy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then write a new policy. The whole point of having policies is that you can write different ones. The default policy does not really cater to this use case, but a policy is a really simple object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without
set_running_loopandget_running_loophow will the policy know precisely what's going on? All I'm saying is thatget_event_loopandset_event_loopare kind of detached from the loop's lifecycle; policy doesn't receive any notification if a loop was stopped or run again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.