0

I have written very basic python code for threading...

import time
import thread

def print_hello(name,delay):
    while 1:
        print name
        time.sleep(delay)

        try:
            thread.start_new_thread(print_hello, ("frist",1,))
            thread.start_new_thread(print_hello, ("second",2,))
        except:
            print "unable to start thread"

        time.sleep(4)
        print "hello"

which output is:

second
frist
frist
second
frist
frist
hello

Exception:

Unhandled exception in thread started by 
  sys.excepthook is missing
  lost sys.stderr

Unhandled exception in thread started by 
  sys.excepthook is missing
  lost sys.stderr

my query is:

  1. why second coming 1st?
  2. why the exception?
Sander Steffann
  • 9,509
  • 35
  • 40

2 Answers2

3

Simple answer: Because threading is tricksy.

Zero-th of all, the code entered above can't possibly produce the output you show. I think what you really intended was for everything from the try: down to be out-dented to the same level as def. When I make that change, and run your code I get results similar to yours, sometimes.

To address your frist question: Frist comes second because time.sleep() does not guarantee that it will delay for exactly the specified time. See the docs here. When I run your code, sometimes frist comes second and sometimes frist comes first, and sometimes they show up together.

To address your second question: Because your two background threads get the rug pulled out from under them when the main program exits.

The two threads run ok while the main program executes the sleep and print. Once those complete, the program tries to exit. At about the same time, the two threads are trying to print their output.

I found that if you increase the sleep(4) to sleep(5.5) it runs a lot cleaner because neither thread is trying to print while the program is exiting.

In real life what you should do is have a mechanism to signal the background threads to break out of their infinte loop and exit before the main program exits, then have the main program wait (by calling join()) until the threads have stopped before exiting.

Read up about threading and join.

jwygralak67
  • 924
  • 7
  • 13
  • Hello,I am new in threding programs.I got the point here,but i also have some basic query how does thread actually work? is it that all thred is allocated to a seperate stack,i am surprise why thread stops once program execution stops. – user2743375 Jan 17 '14 at 06:13
  • @user2743375 I don't know a lot about the internal details. Perhaps this question (http://stackoverflow.com/questions/31340/how-do-threads-work-in-python-and-what-are-common-python-threading-specific-pit) will help. – jwygralak67 Jan 17 '14 at 17:41
  • 1
    +1 for consistent use of "frist" :D – André Laszlo Mar 10 '14 at 09:36
1

This is an infinite loop.

The print_hello function starts a thread which starts a thread which starts a thread...

Bad things will happen and eventually it will crash.

jossgray
  • 497
  • 6
  • 20