Re: subprocess.popen how wait complete open process

2022-08-22 Thread Eryk Sun
On 8/21/22, 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

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 = 0x_
WAIT_FAILED = 0x_
WAIT_TIMEOUT = 0x_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()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess.popen how wait complete open process

2022-08-21 Thread Chris Angelico
On Mon, 22 Aug 2022 at 13:41, Dan Stromberg  wrote:
>
>
>
> On Sun, Aug 21, 2022 at 2:05 PM Chris Angelico  wrote:
>>
>> On Mon, 22 Aug 2022 at 05:39, 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
>> >
>>
>> 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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess.popen how wait complete open process

2022-08-21 Thread Dan Stromberg
On Sun, Aug 21, 2022 at 2:05 PM Chris Angelico  wrote:

> On Mon, 22 Aug 2022 at 05:39, 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
> >
>
> First you have to define "fully open". How would you know?
>

If you're on X11, you could conceivably use:
 xwininfo -tree -root
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess.popen how wait complete open process

2022-08-21 Thread Chris Angelico
On Mon, 22 Aug 2022 at 05:39, 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
>

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

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess.popen how wait complete open process

2022-08-21 Thread Paul Bryan
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

-- 
https://mail.python.org/mailman/listinfo/python-list


subprocess.popen how wait complete open process

2022-08-21 Thread simone zambonardi
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
-- 
https://mail.python.org/mailman/listinfo/python-list