I have a heavily multi threaded engine that might be executing anywhere up to 40 or so jython scripts concurrently (generally it's less than 10).  I create a single PythonInterpreter() and manipulate it's settings in each thread since it appears that the interpreter is merely an interface to an engine that gets at it's state based upon a thread local system state object.


Anyway, the problem that I'm running into is that I want to gather any print output that a script generates and keep each on separate so that I can log this appropriately, to do this I create a StringWriter and then do an interpreter.setOut with that writer and gather the output after the script completes.  What I'm seeing is that generally this works but fairly often I'm seeing garbled output in the writers where the output of a number of scripts is mixed up in the buffer.


Am I going about this wrong?


Failing that, is it possible to simply override the print statement and have it call one of my own java functions?  I'd rather do this than go through all of my scripts changing a print statement into a print function.


Any suggestions?




"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian W. Kernighan