multi-threaded app hangs

Jp Calderone exarkun at intarweb.us
Wed Dec 10 09:18:27 EST 2003


On Wed, Dec 10, 2003 at 02:18:58PM +0530, Sudipto Biswas wrote:
> Hi,
> 
> I have written a multi-threaded (about 8 threads) networking application
> with a text-mode interface. It writes to a trace-file in the background.
> Additionally I have also given some debug prints to STDOUT from different
> threads. The program runs fine on Python-2.2.2/RH 8.0.
> 
> But when I move to Python-2.2.2/RH-9.0 the program behaves oddly. It stops
> suddenly and *does not* give the continueous stream of expected output in
> STDOUT and tracefile.
> 
> On pressing ctrl-Z the hanged program gets suspended. On giving 'fg'
> command the suspended program comes to foreground, AND AGAIN RUNS FOR
> SOMETIMES (generates stream of text in STDOUT and tracefile) before it
> hangs again. This way ctrl-Z and 'fg' if pressed on hang, helps the
> program to run further.
> 
> I am using the 'Thread' class to create objects which run as separate
> threads.
> 
> Has anybody faced a similar problem?

  Often, threaded programs will behave in mysterious ways.  This is due to
something called a "race condition", where the thread which reaches a
particular section of code varies, leading to different behavior each time
that particular section of code is run.

  One way to prevent this is to use locks, so that code is always run in the
same order.

  Another way to prevent it is to simply avoid using threads.  For a network
application, this is fairly simple.  Just use non-blocking or asynchronous
I/O.  In the standard library, asyncore helps to do this.  As a third party
module, Twisted (http://www.twistedmatrix.com) helps a *lot* more.

  Jp





More information about the Python-list mailing list