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 create new Event Loops on Threads #84988

Closed
chexca mannequin opened this issue May 28, 2020 · 4 comments
Closed

Allow to create new Event Loops on Threads #84988

chexca mannequin opened this issue May 28, 2020 · 4 comments
Labels
3.10 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@chexca
Copy link
Mannequin

chexca mannequin commented May 28, 2020

BPO 40811
Nosy @tiran, @asvetlov, @1st1, @iritkatriel, @chexca
PRs
  • bpo-40811: Allowing to create event loops on threads #20500
  • 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 2022-03-17.13:16:16.702>
    created_at = <Date 2020-05-28.17:54:39.337>
    labels = ['type-bug', '3.10', 'expert-asyncio']
    title = 'Allow to create new Event Loops on Threads'
    updated_at = <Date 2022-03-17.13:16:16.701>
    user = 'https://github.com/chexca'

    bugs.python.org fields:

    activity = <Date 2022-03-17.13:16:16.701>
    actor = 'asvetlov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-03-17.13:16:16.702>
    closer = 'asvetlov'
    components = ['asyncio']
    creation = <Date 2020-05-28.17:54:39.337>
    creator = 'Christian Exposito'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40811
    keywords = ['patch']
    message_count = 4.0
    messages = ['370257', '370261', '393959', '415410']
    nosy_count = 6.0
    nosy_names = ['christian.heimes', 'asvetlov', 'python-dev', 'yselivanov', 'iritkatriel', 'Christian Exposito']
    pr_nums = ['20500']
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40811'
    versions = ['Python 3.10']

    @chexca
    Copy link
    Mannequin Author

    chexca mannequin commented May 28, 2020

    Right now, Async IO module is not automatically creating a new event loop on threads that are not the main thread (https://github.com/python/cpython/blob/master/Lib/asyncio/events.py#L638), but it should be interesting to do it. For example, WSGI applications handle web requests by spawning a new thread. If we allow Async IO module to create event loops on those threads, we will improve its adoption for developing web applications.

    @chexca chexca mannequin added 3.10 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error labels May 28, 2020
    @tiran
    Copy link
    Member

    tiran commented May 28, 2020

    The default policy of asyncio deliberately limits automatic creation of event loops to the main thread. You can't just mix asyncio with a threaded web application and expect it to work correctly.

    See

    class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
    """Default policy implementation for accessing the event loop.
    In this policy, each thread has its own event loop. However, we
    only automatically create an event loop by default for the main
    thread; other threads by default have no event loop.
    Other policies may have different rules (e.g. a single global
    event loop, or automatically creating an event loop per thread, or
    using some other notion of context to which an event loop is
    associated).
    """

    In this policy, each thread has its own event loop.  However, we
    only automatically create an event loop by default for the main
    thread; other threads by default have no event loop.
    
    Other policies may have different rules (e.g. a single global
    event loop, or automatically creating an event loop per thread, or
    using some other notion of context to which an event loop is
    associated).
    

    @iritkatriel
    Copy link
    Member

    Should this be closed as rejected or is there further discussion?

    @asvetlov
    Copy link
    Contributor

    I suggest closing.
    The documentation explicitly describes how asyncio works with threads.

    @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.10 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants