[python-win32] win32 equivalent for "my.exe > stdout.txt &"
Moray B. Grieve
mbg@apama.com
Thu, 14 Mar 2002 14:55:47 -0000
This is a multi-part message in MIME format.
------_=_NextPart_001_01C1CB68.5309F756
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> 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:=20
>=20
>=20
> startupInfo =3D win32process.STARTUPINFO()
>=20
> startupInfo.hStdInput =3D win32api.GetStdHandle(STD_INPUT_HANDLE);
> startupInfo.hStdError =3D win32api.GetStdHandle(STD_ERR_HANDLE);
> startupInfo.hStdOutput =3D win32file.CreateFile("stdout.txt",=20
> win32con.GENERIC_WRITE,
> 0,=20
> None,=20
> win32con.CREATE_ALWAYS,=20
> win32con.FILE_FLAG_WRITE_THROUGH,=20
> 0 ) =20
> startupInfo.dwFlags =3D win32con.STARTF_USESTDHANDLES
> =20
> hProcess, hThread, dwPid, dwTid =3D win32process.CreateProcess(
> None, # appName
> "my.exe", # commandLine
> None, # processAttributes
> None, # threadAttributes
> 1, # bInheritHandles
> 0, # dwCreationFlags
> None, # newEnvironment
> None, # currentDirectory
> startupInfo # startupInfo
> )
>=20
> 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.
>=20
> Thanks,=20
> Moray Grieve
------_=_NextPart_001_01C1CB68.5309F756
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
6.0.5762.3">
<TITLE>win32 equivalent for "my.exe > stdout.txt =
&"</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P><FONT SIZE=3D2 FACE=3D"Arial">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: =
</FONT></P>
<BR>
<P><FONT SIZE=3D2 FACE=3D"Arial">startupInfo =3D =
win32process.STARTUPINFO()</FONT>
</P>
<P><FONT SIZE=3D2 FACE=3D"Arial">startupInfo.hStdInput =3D =
win32api.GetStdHandle(STD_INPUT_HANDLE);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">startupInfo.hStdError =3D =
win32api.GetStdHandle(STD_ERR_HANDLE);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">startupInfo.hStdOutput =3D =
win32file.CreateFile("stdout.txt", </FONT>
<BR> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Arial"> =
win32con.GENERIC_WRITE,</FONT>
<BR> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Arial"> 0, </FONT>
<BR> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Arial"> None, </FONT>
<BR> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Arial"> win32con.CREATE_ALWAYS, =
</FONT>
<BR> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Arial"> =
win32con.FILE_FLAG_WRITE_THROUGH, </FONT>
<BR> =
=
=
=
<FONT SIZE=3D2 =
FACE=3D"Arial"> 0 =
) </FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">startupInfo.dwFlags =3D =
win32con.STARTF_USESTDHANDLES</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial"> =
</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">hProcess, hThread, dwPid, dwTid =3D =
win32process.CreateProcess(</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; None, # =
appName</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; "my.exe", # commandLine</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; None, # =
processAttributes</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; None, # =
threadAttributes</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; =
1,  =
; # bInheritHandles</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; =
0,  =
; # dwCreationFlags</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; None, # =
newEnvironment</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; None, # =
currentDirectory</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; startupInfo # startupInfo</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial"> &nbs=
p; )</FONT>
</P>
<P><FONT SIZE=3D2 FACE=3D"Arial">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.</FONT></P>
<P><FONT SIZE=3D2 FACE=3D"Arial">Thanks, </FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">Moray Grieve</FONT>
</P>
</BODY>
</HTML>
------_=_NextPart_001_01C1CB68.5309F756--