[python-win32] win32 equivalent for "my.exe > stdout.txt &"
Noah Spurrier
noah@sfbags.com
Mon, 18 Mar 2002 11:20:13 -0800
This is a big problem with Windows.
I have found it very hard to capture output from a child program that writes to stdout.
Is your child process a Python script?
You mentioned flushing the buffer, but does the child process flush stdout (sys.stdout.flush() )
or did you mean that the parent flushes it?
When your child terminates and the parent closes the file does it get written?
Make sure the parent actually closes the file.
I have had problems with files that are not explicitly closed.
Windows also has a whole API dedicated to Console applications.
It's horrible. I spent a lot of time fiddling with it. It looked like it would do what I needed,
but it turned out to be a bad trail. But if you are adventurous then look for
"Character-Mode Applications" and "Console Reference" on MSDN.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/conchar_5ehx.asp
You might also trying talking to the guy that maintains the Windows version of Expect:
http://bmrc.berkeley.edu/people/chaffee/expectnt.html
This is an application that controls processes through stdou/stdin.
Maybe you can learn from his technique. This file discusses some of
the techniques:
ftp://bmrc.berkeley.edu/pub/winnt/tcltk/expect/README.NT
Can you modify the source code of your child process?
If you can modify the code, then you could have it write directly to
a file or named pipe instead of stdout. You could also have it write
to a database. This is what I ended up doing.
None of these ideas helps if you are trying to interface with
a precompiled EXE or any application that you cannot modify.
I would be very interested to find out if you ever solve this problem.
Yours,
Noah
-----Original Message-----
From: python-win32-admin@python.org [mailto:python-win32-admin@python.org]On Behalf Of Moray B. Grieve
Sent: Thursday, March 14, 2002 6:56 AM
To: python-win32@python.org
Subject: [python-win32] win32 equivalent for "my.exe > stdout.txt &"
I want to launch a child process from within a python script which will run in the background with all stdout redirected to a file
i.e. a unix equivalent of "my.exe > stdout.txt &". To do this I have been trying something like:
startupInfo = win32process.STARTUPINFO()
startupInfo.hStdInput = win32api.GetStdHandle(STD_INPUT_HANDLE);
startupInfo.hStdError = win32api.GetStdHandle(STD_ERR_HANDLE);
startupInfo.hStdOutput = win32file.CreateFile("stdout.txt",
win32con.GENERIC_WRITE,
0,
None,
win32con.CREATE_ALWAYS,
win32con.FILE_FLAG_WRITE_THROUGH,
0 )
startupInfo.dwFlags = win32con.STARTF_USESTDHANDLES
hProcess, hThread, dwPid, dwTid = win32process.CreateProcess(
None, # appName
"my.exe", # commandLine
None, # processAttributes
None, # threadAttributes
1, # bInheritHandles
0, # dwCreationFlags
None, # newEnvironment
None, # currentDirectory
startupInfo # startupInfo
)
I was hoping that by setting startupInfo.hStdOutput to the newly created file handle, writes to stdout by the child process would
automatically be written to file - however this does not seem to tbe the case. Although stdout does get redirected somewhere, it
does not go to the file and even flushing the handle does not seem to solve the problem. Am I being to naive in this approach? I
have tried all different flags for the win32file.CreateFile and win32process.CreateProcess methods. Do I need to set up the child
stdout to a pipe, and manage the read end of the pipe in a thread? I'm a novice in this area so would appreciate any help.
Thanks,
Moray Grieve