|
From: Joseph S. B. I. <jo...@ba...> - 2001-03-20 01:41:37
|
Brian Zimmer a =E9crit:
> Here's an extremely simple example (by no means production, but could=
> get you on your way):
Here's what I've been using.
For outwriter and errwriter, one can pass either PrintStreams,
OutputStreamWriters, or (if you don't care about the output) None.
execvbuf =3D zeros(8192, "c")
def execv(name, outwriter, errwriter):
# I don't know why we need to check for PrintStream,
# given that instanceof(PrintStream, OutputStream)...
# or maybe I need a mix of isinstance() and issubclass()...
#
if isinstance(outwriter, OutputStream) or isinstance(outwriter, Pri=
ntStream):
outwriter =3D OutputStreamWriter(outwriter)
if isinstance(errwriter, OutputStream) or isinstance(errwriter, Pri=
ntStream):
errwriter =3D OutputStreamWriter(errwriter)
process =3D getRuntime().exec(name)
stdout =3D InputStreamReader(process.getInputStream())
stderr =3D InputStreamReader(process.getErrorStream())
while 1:
if stdout <> None:
if stdout.ready():
n =3D stdout.read(execvbuf)
if n =3D=3D -1:
stdout =3D None
print "stdout now None"
continue
if outwriter <> None:
outwriter.write(execvbuf, 0, n)
continue
if stderr <> None:
if stderr.ready():
n =3D stderr.read(execvbuf)
if n =3D=3D -1:
stderr =3D None
print "stderr now None"
continue
if errwriter <> None:
errwriter.write(execvbuf, 0, n)
continue
try:
status =3D process.exitValue()
if status <> 0:
print "%s returned status %d" % (name, status)
return status
except:
sleep(200)
|