I have a service that I used to run in Python at the command-line that I'm now trying to use within Java.  I don't know whether code or explanation is better - I'll jump in with code, I guess:

--- Clip from TNOPsim.py; most of this is standard from TCP server templates

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    print 'ThreadedTCPRequestHandler'

    def handle(self):

        if cli.debugFlag == True:

            print "Gateway connected."

        # does stuff handling the data to / from connection


class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):

    allow_reuse_address = True


server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)

server_thread = threading.Thread(target=server.serve_forever)

server_thread.setDaemon(True)

server_thread.start()


--- Clip from my TnopsimAgent

    /**

     * Stream associated with the STDIN of TNOPsim.

     */

    private InputStream scriptInput;


    /**

     * Stream associated with the STDOUT of TNOPsim so we can read response

     * data.

     */

    private OutputStream scriptOutput;


...


    private final Runnable pythonRunnable = new Runnable() {

        public void run() {

            PythonInterpreter python = new PythonInterpreter();

            python.setIn(scriptInput);

            python.setOut(scriptOutput);

            python.execfile(ClassLoader.getSystemResourceAsStream("Lib/TNOPsim.py"));

        }

    };


The trouble is, that after handle() returns, scriptInput and scriptOutput are no good for communicating with the PythonInterpreter instance anymore.  I'd like it to behave like the command-line version where the prints in handle() go to STDOUT but in my case, they would go to my scriptOutput stream.  I can tell that new connections are still accepted but they appear to get a different stream after the first handle() returns and that doesn't go to scriptOutput.

Can you see where I'm going wrong?

- jeremiah


The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail. Get busy.