using print() with multiprocessing and pythonw

I

Isaac Gerg

I launch my program with pythonw and begin it with the code below so that all my print()'s go to the log file specified.

if sys.executable.find('pythonw') >=0:
# Redirect all console output to file.
sys.stdout = open("pythonw - stdout stderr.log",'w')
sys.stderr = sys.stdout

During the course of my program, I call multiprocessing.Process() and launch a function several times. That function has print()'s inside (which are from warnings being printed by python). This printing causes the multiprocess to crash. How can I fix my code so that the print()'s are supressed. Iwould hate to do a warnings.filterwarnings('ignore') because when I unit test those functions, the warnings dont appear.

Thanks in advance,
Isaac
 
I

Isaac Gerg

Thanks for the reply Bill. The problem is the text i am getting is from a python warning message, not one of my own print() function calls.
 
C

Chris Angelico

Thanks for the reply Bill. The problem is the text i am getting is from a python warning message, not one of my own print() function calls.

Since sys.stdout is just an object, you could replace it with
something that redirects its write() calls to the logging function.
Might be a bit hacky, but it should work.

BTW, "sys.executable.find('pythonw') >=0" should be able to be spelled
"'pythonw' in sys.executable".

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top