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

Event loop implementation docs advertise set_event_loop which doesn't work with asyncio.run #82604

Closed
hniksic mannequin opened this issue Oct 9, 2019 · 4 comments · Fixed by #97755
Closed

Event loop implementation docs advertise set_event_loop which doesn't work with asyncio.run #82604

hniksic mannequin opened this issue Oct 9, 2019 · 4 comments · Fixed by #97755
Assignees
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes docs Documentation in the Doc dir topic-asyncio

Comments

@hniksic
Copy link
Mannequin

hniksic mannequin commented Oct 9, 2019

BPO 38423
Nosy @hniksic, @asvetlov, @1st1

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2019-10-09.17:14:39.175>
labels = ['3.8', 'docs', '3.7', '3.9', 'expert-asyncio']
title = "Event loop implementation docs advertise set_event_loop which doesn't work with asyncio.run"
updated_at = <Date 2020-01-27.15:18:48.508>
user = 'https://github.com/hniksic'

bugs.python.org fields:

activity = <Date 2020-01-27.15:18:48.508>
actor = 'Cilyan Olowen'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation', 'asyncio']
creation = <Date 2019-10-09.17:14:39.175>
creator = 'hniksic'
dependencies = []
files = []
hgrepos = []
issue_num = 38423
keywords = []
message_count = 4.0
messages = ['354288', '354289', '360770', '360772']
nosy_count = 5.0
nosy_names = ['hniksic', 'asvetlov', 'docs@python', 'yselivanov', 'Cilyan Olowen']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue38423'
versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

@hniksic
Copy link
Mannequin Author

hniksic mannequin commented Oct 9, 2019

The docs of SelectorEventLoop and ProactorEventLoop contain examples that call asyncio.set_event_loop:

      selector = selectors.SelectSelector()
      loop = asyncio.SelectorEventLoop(selector)
      asyncio.set_event_loop(loop)

But this won't have any effect on code that uses asyncio.run(), because asyncio.run() creates a fresh event loop. Since asyncio.run() is the recommended way to execute async code and is used consistently throughout the documentation, this might be confusing to someone who tries to e.g. use the proactor loop on Windows.

I propose the following:

  • add a disclaimer that instantiating the event loop won't affect calls to asyncio.run(), and that loop.run_until_complete() must be used; and

  • link to asyncio.set_event_loop_policy(), which does work with asyncio.run(), but doesn't allow fine-tuning the details, such as which selector to use for the SelectorEventLoop.

@hniksic hniksic mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes labels Oct 9, 2019
@hniksic hniksic mannequin assigned docspython Oct 9, 2019
@hniksic hniksic mannequin added docs Documentation in the Doc dir topic-asyncio labels Oct 9, 2019
@hniksic hniksic mannequin changed the title Event loop implementation docs advertise set_event_loop Event loop implementation docs advertise set_event_loop which doesn't work with asyncio.run Oct 9, 2019
@1st1
Copy link
Member

1st1 commented Oct 9, 2019

Yes, docs updates sound good.

@CilyanOlowen
Copy link
Mannequin

CilyanOlowen mannequin commented Jan 27, 2020

It doesn't seem to work either for asyncio.get_event_loop, especially when using asyncio.wait(), the loop complains that

"RuntimeError: Task <Task pending coro=<Event.wait() running at lib\asyncio\locks.py:293> cb=[_wait.<locals>._on_completion() at lib\asyncio\tasks.py:440]> got Future <Future pending> attached to a different loop"

@CilyanOlowen
Copy link
Mannequin

CilyanOlowen mannequin commented Jan 27, 2020

My bad, I did not check that asyncio.Event() was called before the application has a chance to properly set_event_loop with custom one.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@kumaraditya303 kumaraditya303 self-assigned this Oct 3, 2022
@kumaraditya303 kumaraditya303 added 3.11 only security fixes 3.10 only security fixes 3.12 bugs and security fixes and removed 3.9 only security fixes 3.8 only security fixes 3.7 (EOL) end of life labels Oct 3, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 4, 2022
(cherry picked from commit 53503ff)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 4, 2022
(cherry picked from commit 53503ff)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
miss-islington added a commit that referenced this issue Oct 4, 2022
(cherry picked from commit 53503ff)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
miss-islington added a commit that referenced this issue Oct 4, 2022
(cherry picked from commit 53503ff)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
pablogsal pushed a commit that referenced this issue Oct 22, 2022
(cherry picked from commit 53503ff)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes docs Documentation in the Doc dir topic-asyncio
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants