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

Allow to provide custom exception handler to asyncio.run() #85374

Closed
tomaszdrozdz mannequin opened this issue Jul 3, 2020 · 9 comments
Closed

Allow to provide custom exception handler to asyncio.run() #85374

tomaszdrozdz mannequin opened this issue Jul 3, 2020 · 9 comments
Labels
3.8 only security fixes topic-asyncio type-feature A feature request or enhancement

Comments

@tomaszdrozdz
Copy link
Mannequin

tomaszdrozdz mannequin commented Jul 3, 2020

BPO 41202
Nosy @asvetlov, @1st1, @aeros, @tomaszdrozdz
PRs
  • bpo-41202: Allow to provide custom exception handler to asyncio.run() #21295
  • 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 = <Date 2020-07-11.03:08:49.273>
    created_at = <Date 2020-07-03.14:26:06.886>
    labels = ['type-feature', '3.8', 'expert-asyncio']
    title = 'Allow to provide custom exception handler to asyncio.run()'
    updated_at = <Date 2020-07-11.03:08:49.272>
    user = 'https://github.com/tomaszdrozdz'

    bugs.python.org fields:

    activity = <Date 2020-07-11.03:08:49.272>
    actor = 'aeros'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-07-11.03:08:49.273>
    closer = 'aeros'
    components = ['asyncio']
    creation = <Date 2020-07-03.14:26:06.886>
    creator = 'tomaszdrozdz'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41202
    keywords = ['patch']
    message_count = 9.0
    messages = ['372934', '373181', '373226', '373227', '373348', '373390', '373408', '373463', '373508']
    nosy_count = 4.0
    nosy_names = ['asvetlov', 'yselivanov', 'aeros', 'tomaszdrozdz']
    pr_nums = ['21295']
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue41202'
    versions = ['Python 3.8']

    @tomaszdrozdz
    Copy link
    Mannequin Author

    tomaszdrozdz mannequin commented Jul 3, 2020

    I wish we had:

    asyncio.run(coro, *, debug=False, excepton_handler=None)

    so we could provide custome exception handler function for the loop.

    @tomaszdrozdz tomaszdrozdz mannequin added 3.8 only security fixes topic-asyncio type-feature A feature request or enhancement labels Jul 3, 2020
    @tomaszdrozdz tomaszdrozdz mannequin changed the title Allo to provide custom exception handler to asyncio.run() Allow to provide custom exception handler to asyncio.run() Jul 3, 2020
    @tomaszdrozdz tomaszdrozdz mannequin changed the title Allo to provide custom exception handler to asyncio.run() Allow to provide custom exception handler to asyncio.run() Jul 3, 2020
    @1st1
    Copy link
    Member

    1st1 commented Jul 6, 2020

    The idiomatic way:

    async def main():
      loop = asyncio.get_running_loop()
      loop.set_exception_handler(...)
      # other code
    
    asyncio.run(main())

    We don't want to add new arguments to asyncio.run as there would be too many.

    @tomaszdrozdz
    Copy link
    Mannequin Author

    tomaszdrozdz mannequin commented Jul 7, 2020

    https://docs.python.org/3/library/asyncio-eventloop.html#error-handling-api

    Here we can read:

    Application developers should typically use the high-level asyncio functions, such as asyncio.run(), and should rarely need to reference the loop object or call its methods. This section is intended mostly for authors of lower-level code, libraries, and frameworks, who need finer control over the event loop behavior.

    So as I understand this - I should not use
    asyncio.get_running_loop
    loop.set_exception_handler(...)

    Or maybe event loop should not be in "Low level api" ???

    @asvetlov
    Copy link
    Contributor

    asvetlov commented Jul 7, 2020

    I agree with Yuri.

    Usually, you don't need overriding of the default exception handler.
    Indeed, if you really need this low-level API I see nothing wrong with asyncio.get_running_loop() call.

    @aeros
    Copy link
    Contributor

    aeros commented Jul 8, 2020

    Yep, having to set a custom exception handler definitely constitutes as needing "finer control over the event loop behavior". There's absolute nothing wrong with using the low-level API when you need further customization, but we try to minimize the high-level API as much as possible to make it simple for the average user. I suspect that the majority of asyncio users don't have a need to customize the exception handler beyond the default settings.

    @tomaszdrozdz
    Copy link
    Mannequin Author

    tomaszdrozdz mannequin commented Jul 9, 2020

    OK.
    I understand.

    So how about maybe:

        def run(main, *, debug=False, loop=None):  
            ...  
        
            if loop:  
                loop = events.new_event_loop()

    So we could customize loop like:

        loop = events.new_event_loop()  
        loop.set_XXX(...)  
        asyncio.run(my_coro, loop=loop)  

    Just what tehn with debug parameter ?

    @1st1
    Copy link
    Member

    1st1 commented Jul 9, 2020

    So how about maybe:

    That wouldn't work. You still haven't explained what's wrong with calling loop = asyncio.get_running_loop() inside async def main(). That literally solves all problems without the need of us modifying any APIs.

    @tomaszdrozdz
    Copy link
    Mannequin Author

    tomaszdrozdz mannequin commented Jul 10, 2020

    I just wanted to call

        def main():
            asyncio.run(...)

    But I can go with Your aproach.
    Thanks for discusion.

    Should I set status for this issue for closed with resolution rejected ?

    Should I delete branch on my forked git repo ?
    Can I delete my forked git repo ?

    @aeros
    Copy link
    Contributor

    aeros commented Jul 11, 2020

    Should I set status for this issue for closed with resolution rejected ?

    I'll proceed with closing the issue.

    Should I delete branch on my forked git repo ?
    Can I delete my forked git repo ?

    Might as well delete the branch, but the forked repo might be useful to keep around should you choose to open a PR to CPython in the future (although you can also just create a new one later). If you are interested in working on something else in the future, I'd recommend looking at the "newcomer friendly"/"easy" issues, or just looking for an existing open issue that you're interested in helping with. In most cases, working with a clearly defined issue is much easier than trying to propose a new one and getting it merged.

    @aeros aeros closed this as completed Jul 11, 2020
    @aeros aeros closed this as completed Jul 11, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes topic-asyncio type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants