sys.stdout redirection

Holger Joukl Holger.Joukl at LBBW.de
Mon Jul 19 03:24:52 EDT 2004


>    Iam redirecting the sys.stdout to the file. iam doing this is multiple
thread.
>While running my application exception throws "Value.error I/O Operation
on closed file"
>
>import sys
>
>boolStdOut = True
>if boolStdOut == 1:
>    print "This is stdout"
>    oldStd = sys.stdout
>    sys.stdout = open("c:\\std.out", "w")
>    print "This is Redirected stdout to the file"
>    sys.stdout.flush()
>    sys.stdout.close()
>    sys.stdout = oldStd--

If you are entering this code from multiple threads, a thread switch can
result in
a situation where one thread runs into the print/flush lines after another
one has
just closed the file you opened and assigned to stdout.
Guard it with a lock, please see python docs (threading module).

Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
den Inhalt der E-Mail als Hardcopy an.

The contents of this  e-mail are confidential. If you are not the named
addressee or if this transmission has been addressed to you in error,
please notify the sender immediately and then delete this e-mail.  Any
unauthorized copying and transmission is forbidden. E-Mail transmission
cannot be guaranteed to be secure. If verification is required, please
request a hard copy version.



More information about the Python-list mailing list