473,412 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Issues With Threading

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

Jul 21 '05 #1
1 1457
This is a complete shot in the dark but you might want to
sys.stdout.flush() between print calls?

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Batista, Facundo | last post by:
People, executing the following code from command line, it works as expected (you see "th1 - 2" *always* after "th2 - 2"). But when I execute from IDLE one of two things happens: - Everything...
3
by: Philip V Pham | last post by:
These questions apply to std vector, map, and cout: I am uncertain of the thread safety for reading/writing for std templates. I know if all threads are reading concurrently, it is thread...
5
by: Michele Simionato | last post by:
I am getting a strange error with this script: $ cat doctest-threads.py """ >>> import time, threading >>> def example(): .... thread.out = .... while thread.running: .... ...
0
by: KC | last post by:
Could someone elaborate on any possbile issue storing an XmlSchema object statically for reuse. I am dowloading, reading and compiling a schema from an external url. I am then saving the...
0
by: omyek | last post by:
Heh, I know some of you have read the subject and are wondering what in the heck I'm doing. Well, for all intensive purposes, I've had to embed a WebBrowser control into a DLL and reference that...
0
by: Russell Warren | last post by:
I've got a case where I need to tweak the implementation of a default python library due to what I consider to be an issue in the library. What is the best way to do this and make an attempt to...
7
by: Wayne Pedersen | last post by:
Folks, I'm hosting through 1and1.com. I don't have a lot of experience deploying asp.net apps on shared webservers. I keep getting the following error message when trying to have my ASP.Net...
12
by: =?Utf-8?B?SmFyb2Q=?= | last post by:
Hi! I've done some code using threading just to check if writing code improperly can cause serious issues when using somehow safe data types. So in my understanding a stack ( collection type )...
2
by: James Calivar | last post by:
I'm a newbie trying to write a script that uses threads. I'm right now a little bit stuck in understanding why the code snippet I wrote doesn't seem to be entering the function defined in the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.