Issues With Threading

Your Friend myfriendmyfriend at gmail.com
Tue Jul 12 11:42:17 EDT 2005


Hello All,

I'm having issues capturing the output from a program while using
threading.  Program runs ok when I run without threading.  Here's my
Python code and the Java class that is called by it.

Python :

#!/usr/bin/python

import popen2
import threading

for id in range( 10 ) :
        ( err_out, stdin ) = popen2.popen4( ( '/usr/bin/java JavaTest
%s' ) % ( id ) )
        for line in err_out.readlines() :
                print line,

def test( id ) :
        print "Called %s" % ( id )
        ( err_out, stdin ) = popen2.popen4( ( '/usr/bin/java JavaTest
%s' ) % ( id ) )
        for line in err_out.readlines() :
                print line,

#for id in range( 10 ) :
#        thread = threading.Thread( target=test, args=( [ id ] ) )
#        thread.start()

Java :

import java.util.Date;

public class JavaTest {
        public static void main( String args[] ) {
                System.out.println( args[0] + " - " + new
Date().toString() );
        }
}

When I run without threading, I get the correct output :

0 - Tue Jul 12 11:33:51 EDT 2005
1 - Tue Jul 12 11:33:52 EDT 2005
2 - Tue Jul 12 11:33:52 EDT 2005
3 - Tue Jul 12 11:33:53 EDT 2005
4 - Tue Jul 12 11:33:53 EDT 2005
5 - Tue Jul 12 11:33:54 EDT 2005
6 - Tue Jul 12 11:33:54 EDT 2005
7 - Tue Jul 12 11:33:54 EDT 2005
8 - Tue Jul 12 11:33:54 EDT 2005
9 - Tue Jul 12 11:33:54 EDT 2005

When I uncomment the threading section and run again, I see that the
function is called, but the result from the Java code does not get
printed :

Called 0
Called 1
Called 2
Called 3
Called 4
Called 5
Called 6
Called 7
Called 8
Called 9

I hope this is just a nuance that I've run across and it's an easy
solution ... any advice pointing me in the right direction would be
greatly appreciated.

Thanks




More information about the Python-list mailing list