[Python] Why asyncio.get_event_loop method checks if the current thread is the main thread?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    Why get_event_loop method in asyncio (source) is checking if the current thread is the main thread (see my comment in the snippet below)?

    def get_event_loop(self):
    """Get the event loop.

    This may be None or an instance of EventLoop.
    """
    if (self._local._loop is None and
    not self._local._set_called and
    isinstance(threading.current_thread(), threading._MainThread)): # <- I mean this thing here
    self.set_event_loop(self.new_event_loop())
    if self._local._loop is None:
    raise RuntimeError('There is no current event loop in thread %r.'
    % threading.current_thread().name)
    return self._local._loop

    Continue reading...

Compartilhe esta Página