• subprocess.popen how wait complete open process

    From simone zambonardi@21:1/5 to All on Sun Aug 21 02:11:38 2022
    Hi, I am running a program with the punishment subrocess.Popen(...) what I should do is to stop the script until the launched program is fully open. How can I do this? I used a time.sleep() function but I think there are other ways. Thanks

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Bryan@21:1/5 to simone zambonardi on Sun Aug 21 12:48:07 2022
    Sometimes, launching subprocesses can seem like punishment. I don't
    think there is a standard cross-platform way to know when a launched asynchronous process is "fully open" (i.e. fully initialized, accepting
    user input).

    On Sun, 2022-08-21 at 02:11 -0700, simone zambonardi wrote:
    Hi, I am running a program with the punishment subrocess.Popen(...)
    what I should do is to stop the script until the launched program is
    fully open. How can I do this? I used a time.sleep() function but I
    think there are other ways. Thanks

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to simone.zambonardi@gmail.com on Mon Aug 22 07:05:08 2022
    On Mon, 22 Aug 2022 at 05:39, simone zambonardi
    <simone.zambonardi@gmail.com> wrote:

    Hi, I am running a program with the punishment subrocess.Popen(...) what I should do is to stop the script until the launched program is fully open. How can I do this? I used a time.sleep() function but I think there are other ways. Thanks


    First you have to define "fully open". How would you know?

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Dan Stromberg on Mon Aug 22 14:01:01 2022
    On Mon, 22 Aug 2022 at 13:41, Dan Stromberg <drsalists@gmail.com> wrote:



    On Sun, Aug 21, 2022 at 2:05 PM Chris Angelico <rosuav@gmail.com> wrote:

    On Mon, 22 Aug 2022 at 05:39, simone zambonardi
    <simone.zambonardi@gmail.com> wrote:

    Hi, I am running a program with the punishment subrocess.Popen(...) what I should do is to stop the script until the launched program is fully open. How can I do this? I used a time.sleep() function but I think there are other ways. Thanks


    First you have to define "fully open". How would you know?


    If you're on X11, you could conceivably use:
    xwininfo -tree -root


    That's only one possible definition: it has some sort of window. But
    to wait until a program is "fully open", you might have to wait past a
    splash screen until it has its actual application window. Or maybe
    even then, it's not ready for operation. Only the OP can know what
    defines "fully open".

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Stromberg@21:1/5 to rosuav@gmail.com on Sun Aug 21 20:41:43 2022
    On Sun, Aug 21, 2022 at 2:05 PM Chris Angelico <rosuav@gmail.com> wrote:

    On Mon, 22 Aug 2022 at 05:39, simone zambonardi
    <simone.zambonardi@gmail.com> wrote:

    Hi, I am running a program with the punishment subrocess.Popen(...) what
    I should do is to stop the script until the launched program is fully open. How can I do this? I used a time.sleep() function but I think there are
    other ways. Thanks


    First you have to define "fully open". How would you know?


    If you're on X11, you could conceivably use:
    xwininfo -tree -root

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eryk Sun@21:1/5 to simone zambonardi on Mon Aug 22 08:47:06 2022
    On 8/21/22, simone zambonardi <simone.zambonardi@gmail.com> wrote:
    Hi, I am running a program with the punishment subrocess.Popen(...) what I should do is to stop the script until the launched program is fully open.
    How can I do this? I used a time.sleep() function but I think there are
    other ways. Thanks

    In Windows, WaitForInputIdle() waits until a thread in a process
    creates one or more windows and its message loop goes idle. Usually
    this is the main UI thread. Console processes are not supported.

    For example:

    import ctypes
    import subprocess

    user32 = ctypes.WinDLL('user32', use_last_error=True)

    INFINITE = 0xFFFF_FFFF
    WAIT_FAILED = 0xFFFF_FFFF
    WAIT_TIMEOUT = 0x0000_0102

    # Waiting on a console process fails with ERROR_NOT_GUI_PROCESS.
    # This case be handled in other ways, depending on the need. ERROR_NOT_GUI_PROCESS = 1471

    user32.WaitForInputIdle.restype = ctypes.c_ulong user32.WaitForInputIdle.argtypes = (ctypes.c_void_p, ctypes.c_ulong)

    def wait_for_input_idle(proc, timeout=None):
    if isinstance(proc, subprocess.Popen):
    handle = int(proc._handle)
    args = p.args
    else:
    handle = int(proc)
    args = ''
    if timeout is None:
    timeout_ms = INFINITE
    elif timeout < 0:
    raise ValueError('timeout cannot be negative')
    else:
    timeout_ms = int(timeout * 1000)
    if timeout_ms >= INFINITE:
    raise OverflowError('timeout is too large')
    status = user32.WaitForInputIdle(handle, timeout_ms)
    if status == WAIT_FAILED:
    raise ctypes.WinError(ctypes.get_last_error())
    elif status == WAIT_TIMEOUT:
    raise subprocess.TimeoutExpired(args, timeout)
    assert status == 0
    return


    if __name__ == '__main__':
    import time
    t0 = time.time()
    p = subprocess.Popen(['pythonw.exe', '-m', 'idlelib'])

    try:
    wait_for_input_idle(p, 5)
    except:
    p.terminate()
    raise

    wait_time = time.time() - t0
    print(f'wait time: {wait_time:.3f} seconds')
    try:
    p.wait(5)
    except subprocess.TimeoutExpired:
    p.terminate()

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)