Skip to content

trio/_subprocess_platform/__init__.py imports entire asyncio on Windows #3551

Open

Description

@LmeSzinc

trio/_subprocess_platform/__init__.py imports asyncio.windows_utils. Since asyncio/__init__.py imports everything, importing trio on windows will result in importing the entire asyncio.

elif os.name == "nt":
import msvcrt
# This isn't exported or documented, but it's also not
# underscore-prefixed, and seems kosher to use. The asyncio docs
# for 3.5 included an example that imported socketpair from
# windows_utils (before socket.socketpair existed on Windows), and
# when asyncio.windows_utils.socketpair was removed in 3.7, the
# removal was mentioned in the release notes.
from asyncio.windows_utils import pipe as windows_pipe
from .._windows_pipes import PipeReceiveStream, PipeSendStream
def create_pipe_to_child_stdin() -> tuple[PipeSendStream, int]:
# for stdin, we want the write end (our end) to use overlapped I/O
rh, wh = windows_pipe(overlapped=(False, True))
return PipeSendStream(wh), msvcrt.open_osfhandle(rh, os.O_RDONLY)
def create_pipe_from_child_output() -> tuple[PipeReceiveStream, int]:
# for stdout/err, it's the read end that's overlapped
rh, wh = windows_pipe(overlapped=(True, False))
return PipeReceiveStream(rh), msvcrt.open_osfhandle(wh, 0)

Problem

  • Trio is positioned as an alternative to asyncio, importing asyncio itself is counterintuitive
  • Importing the entire asyncio would add about 100ms import time

Proposal

Implement windows pipe in trio style completely base on python 3.14 asyncio source. _windows_pipe.py and _windows_cffi.py already provide most api required, reimplementation should only cost about 100 to 200 lines I guess.

Activity

  1. A5rocks commented on Oct 21, 2026

    @A5rocks
    Contributor

    Yep this is around 10% of import cost. It's just annoying + Windows-only so I didn't change it previously. (PS try out python -X importtime to get a more accurate accounting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions