Re: Inheriting a command line - or how to efficiently replace Unix' os.execvpe

Dennis Lee Bieber <[email protected]>
Newsgroups gmane.comp.python.windows
Organization IISS Elusive Unicorn
Message-ID <[email protected]>
On Thu, 13 May 2021 12:34:13 +0200, "Sebastian M. Ernst"
<[email protected]> declaimed the following:

>
>thanks a lot for the explanation. It's amazing how Windows does things
>differently in so many places ...
>

	The Windows NT family (which includes Win7 and later, maybe WinXP) gets
a lot of its internal design from DEC VAX/VMS. Unfortunately, it does it in
a way that knowledge of VMS is barely helpful (no mailboxes [IPC scheme],
global/local event flags [2/2 32-bit bitmaps -- not sure how Windows
wait-for-object fits in])
>If my Python process was "simply idling" / blocking, `ssh` could do its
>thing (fully interactively) in the meantime. Do I understand you
>correctly that this can actually be done?
>
>I have done a few dumb experiments along the lines of ...
>
>```python
>proc = subprocess.Popen(['ssh', 'user@host'], **parameters)
>while proc.poll is None:
>    time.sleep(0.25)
>```
>
>... but I am not managing to specify `parameters` so that I can actually
>interact with `ssh`. Am I on the wrong track ... ?

	What have you tried?
https://docs.python.org/3.8/library/subprocess.html
"""
stdin, stdout and stderr specify the executed program’s standard input,
standard output and standard error file handles, respectively. Valid values
are PIPE, DEVNULL, an existing file descriptor (a positive integer), an
existing file object, and None. PIPE indicates that a new pipe to the child
should be created. DEVNULL indicates that the special file os.devnull will
be used. With the default settings of None, no redirection will occur; the
child’s file handles will be inherited from the parent. Additionally,
stderr can be STDOUT, which indicates that the stderr data from the child
process should be captured into the same file handle as for stdout.
"""

Emphasis "With the default settings of None, no redirection will occur; the
child’s file handles will be inherited from the parent. " sounds like that
would mean your ssh would use that consoles stdxxx I/O streams.

	Also on that page...
"""
 Popen.wait(timeout=None)

    Wait for child process to terminate. Set and return returncode
attribute.
"""

	No need for the loop structure. However, it is also a non-blocking/loop
internally...
"""
Note

The function is implemented using a busy loop (non-blocking call and short
sleeps). Use the asyncio module for an asynchronous wait: see
asyncio.create_subprocess_exec. 
"""

... so all it gains you is a one-liner vs two... And it may (I've not
checked the library source) be implemented in C rather than interpreted
byte-code.


	Beyond that... You might need to dig into the win32 extension library.
http://timgolden.me.uk/pywin32-docs/contents.html
"""
win32console.AllocConsole

AllocConsole()

Creates a new console for the calling process

Comments
Calling process must not already be attached to another console

**************************

win32console.AttachConsole

AttachConsole(ProcessId)

Attaches to console of another process

Parameters

ProcessId : int

    Pid of another process, or ATTACH_PARENT_PROCESS

    Comments
    Calling process must not already be attached to another console

**************************

win32console.FreeConsole

FreeConsole()

Detaches process from its current console
"""

	Hypothetically -- parent could FreeConsole() immediately after starting
child process (letting child inherit console). Trick would be to reattach
(to child console) before child is fully cleaned up.

	Maybe...
"""
win32console.GetConsoleWindow

int = GetConsoleWindow()

Returns a handle to the console's window, or 0 if none exists

Return Value
This function may raise NotImplementedError if it does not exist on the
platform, or a PyHANDLE object with a value of 0. It will never raise a
win32 exception.
""" 
... though I don't see any operation that can use the handle to re-attach.
https://docs.microsoft.com/en-us/windows/console/console-functions (win32
may not implement all -- might be available via ctypes library)

	WaitForSingleObject can take a handle to a process -- and may be a
blocking call rather than busy loop.
https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject
https://flylib.com/books/en/4.419.1.70/1/
Getting a Win32 process handle from a Python subprocess PID would be the
next trick if one doesn't want to wade into Win32 createprocess stuff.




-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	[email protected]    http://wlfraed.microdiversity.freeddns.org/

_______________________________________________
python-win32 mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-win32
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.