I don't see any problem with Jython here. The Jython/Python sys.stdout object is not a new concept, but represents the standard output stream for a process. The standard output is a single resource for the whole process, not a separate resource for each thread.


Do you have any control over the Jython code that runs in your Jython interpreters? Changing sys.stdout is a hack to capture output from code that you can't change, that thinks it is running in its own process by itself. It's simpler to define an output() function in each interpreter that sends output to the right place, if you can do that.




From: jython-users-bounces@lists.sourceforge.net [mailto:jython-users-bounces@lists.sourceforge.net] On Behalf Of Carmelo Scala
Sent: Wednesday, December 12, 2007 10:11 AM
To: Jython-users@lists.sourceforge.net; claudef@br.ibm.com
Subject: Re: [Jython-users] help with PythonInterpeter -> setOut / out in Y


Hi Claude and thanks for your help …
If I have understood it right I shall create a sort of manager that will be responsible of dealing with the output depending on who is going to "print" …
I could probably find a why … but this is going be a really mess in a multi thread environment :-(
I have more than 10 PyInterpreter running in different threads … I shall create some manager which upon an operation will:
-           lock the resource
-           set the out for the interpreter
-          PERFORM ANY ACTION (not just a print … because I’m displaying the output of each single operation)
-          And free the resource …

If I have understood the example right!!!
Shouldn’t probably this be fixed in Jyhton ???


----- Original Message -----

From: claudef@br.ibm.com

To: Jython-users@lists.sourceforge.net

Cc: Carmelo Scala

Sent: Wednesday, December 12, 2007 3:48 PM

Subject: Re: [Jython-users] help with PythonInterpeter -> setOut / out in Y


Dear Carmelo,

 Below a code that illustrates how to redirect an output stream using Jython. This procedure is
used in context with Java servlets and redirects the output stream in  Y, sharing the report
between the web screen and a central report repository. Maybe the sample gives you some
    # redirect_out              
    def redirect_out():
        global _fileObject
        _fileObject = open("C:/../reports/" + report_name + report_date + ".xls", "w")
        sys.stdout = _fileObject
        System.out.println("standard output has been redirected to a file")

    # modified println command  
    def copy_println(myline):
         if (_repository == 'on'):

    # restore_out              
    def restore_out():
        global _fileObject
        sys.stdout = sys.__stdout__
        System.out.println("standard output has been restored back to normal")    

     # redirect ouput
      if (_repository == 'on'):

     copy_println('bla bla bla')

     # switch back to normal out mode


Claude Falbriard
AMS Hortolāndia / SP - Brazil
phone: +55 13 8117 3316
e-mail:  claudef@br.ibm.com

"Carmelo Scala" <carmeloscala@hotmail.com>
Sent by: jython-users-bounces@lists.sourceforge.net

12/12/2007 12:17 PM





[Jython-users] help with PythonInterpeter -> setOut


Hi all,

Hope some one can help me in solving this problem, guess there something I'm
doing wrong .

I have a java program that uses PythonInterpreter and set the out of the
interpreter with the setOut.

What is not clear for me is

-          I create the a pythonInterpreter (by default the out goes on

-          I set the out to a file .

-          I create a new PythonInterpreter .  I WOULD HAVE EXPECTECD THAT
THIS FRESH COPY would have started using again the system.out instead of
sharing the out of the previous interpreter :-(

My applications use different PyInterpreter to perform different operation
on files and redirect on different OutputStream . the problem is that
everything goes in the last OutputStream assigned.

Any help would be appreciated



import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;

import org.python.util.PythonInterpreter;

public class PyTest2
   public PyTest2()


   public void init()
       Properties sysProps = System.getProperties();
       Properties pyPostProps = new Properties();
       if (sysProps.getProperty("python.home") == null)
           String urlStr = "C:\\jython2.2.1";

           if (urlStr != null)
               pyPostProps.put("python.home", urlStr);
               System.out.println("jython home has been set to: "
                       + pyPostProps.getProperty("python.home"));
                       .println("[1] Jython python.home directory has not
been defined correctly. Fix the problem.");
           System.out.println("Jython home set from sys properties to: "
                   + sysProps.getProperty("python.home"));


       // init python interpreter
       PythonInterpreter.initialize(pyPostProps, null, new String[] {});

       PythonInterpreter inter = new PythonInterpreter();


           inter.setOut(new FileOutputStream("c:\\temp\\pyout.log"));
       catch (FileNotFoundException e)

       inter = null;

       System.out.println("Starting second instance ...");

       inter = new PythonInterpreter();
       // the out of this interpreter should go to the System.out instead
it goes in the file

   private void printInfo(PythonInterpreter inter)
       inter.exec("import sys");
       inter.exec("print sys.path");
       inter.exec("print dir ()");
       inter.exec("print 'sys.cachedir=', sys.cachedir");

   public static void main(String[] args)
       PyTest2 unit = new PyTest2();

SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
Jython-users mailing list