Ejemplo n.º 1
0
    def add_win32_handle(self, handle: HANDLE,
                         callback: Callable[[], None]) -> None:
        """
        Add a Win32 handle to the event loop.
        """
        if handle.value is None:
            raise ValueError('Invalid handle.')
        handle_value = handle.value

        loop = get_event_loop()
        self._handle_callbacks[handle_value] = callback

        # Add reader.
        def ready() -> None:
            # Tell the callback that input's ready.
            try:
                callback()
            finally:
                run_in_executor_with_context(wait, loop=loop)

        # Wait for the input to become ready.
        # (Use an executor for this, the Windows asyncio event loop doesn't
        # allow us to wait for handles like stdin.)
        def wait() -> None:
            if self._handle_callbacks.get(handle_value) != callback:
                return

            wait_for_handles([handle])
            loop.call_soon_threadsafe(ready)

        run_in_executor_with_context(wait, loop=loop)
    def add_win32_handle(self, handle: int, callback: Callable[[], None]) -> None:
        """
        Add a Win32 handle to the event loop.
        """
        loop = get_event_loop()
        self._handle_callbacks[handle] = callback

        # Add reader.
        def ready() -> None:
            # Tell the callback that input's ready.
            try:
                callback()
            finally:
                run_in_executor_with_context(wait, loop=loop)

        # Wait for the input to become ready.
        # (Use an executor for this, the Windows asyncio event loop doesn't
        # allow us to wait for handles like stdin.)
        def wait() -> None:
            if self._handle_callbacks.get(handle) != callback:
                return

            wait_for_handles([handle])
            loop.call_soon_threadsafe(ready)

        run_in_executor_with_context(wait, loop=loop)
Ejemplo n.º 3
0
    def add_win32_handle(self, handle: HANDLE,
                         callback: Callable[[], None]) -> None:
        """
        Add a Win32 handle to the event loop.
        """
        handle_value = handle.value

        if handle_value is None:
            raise ValueError("Invalid handle.")

        # Make sure to remove a previous registered handler first.
        self.remove_win32_handle(handle)

        loop = get_event_loop()
        self._handle_callbacks[handle_value] = callback

        # Create remove event.
        remove_event = create_win32_event()
        self._remove_events[handle_value] = remove_event

        # Add reader.
        def ready() -> None:
            # Tell the callback that input's ready.
            try:
                callback()
            finally:
                run_in_executor_with_context(wait, loop=loop)

        # Wait for the input to become ready.
        # (Use an executor for this, the Windows asyncio event loop doesn't
        # allow us to wait for handles like stdin.)
        def wait() -> None:
            # Wait until either the handle becomes ready, or the remove event
            # has been set.
            result = wait_for_handles([remove_event, handle])

            if result is remove_event:
                windll.kernel32.CloseHandle(remove_event)
                return
            else:
                loop.call_soon_threadsafe(ready)

        run_in_executor_with_context(wait, loop=loop)
Ejemplo n.º 4
0
 def ready() -> None:
     # Tell the callback that input's ready.
     try:
         callback()
     finally:
         run_in_executor_with_context(wait, loop=loop)
 def pre_run() -> None:
     run_in_executor_with_context(start)
Ejemplo n.º 6
0
 def pre_run() -> None:
     run_in_executor_with_context(start)
 def ready() -> None:
     # Tell the callback that input's ready.
     try:
         callback()
     finally:
         run_in_executor_with_context(wait, loop=loop)