Issues With Threading

Y

Your Friend

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
 
P

pythonUser_07

This is a complete shot in the dark but you might want to
sys.stdout.flush() between print calls?
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top