I'm stumped on threads 
Author Message
 I'm stumped on threads

Can anyone give me hint about what's wrong with the following program?

#!/usr/local/bin/python
# this is a simple program that uses threads

import thread

def One_Thing(counter):
        identone = threadone.get_ident()
        print "One thing" + counter + ': ' + identone
        counter = counter + 1

def Two_Thing(counter):
        identtwo = threadtwo.get_ident()
        print "Two thing" + counter + ': ' + identtwo
        counter = counter + 1

counter = 1
threadone = thread.start_new_thread(One_Thing, (counter,))
threadtwo = thread.start_new_thread(Two_Thing, (counter,))
print threadone, threadtwo
print counter

it outputs None, None and 1 which makes sense if the two functions
aren't called.

Adam Bjornholm



Mon, 20 Sep 1999 03:00:00 GMT  
 I'm stumped on threads

Quote:
> Try changing the first line of One_Thing and Two_Thing to:
>     identone=thread.get_ident() # same for identtwo...

In addition, you didn't give the threads time to run.  Try insert a
sleep before you print statements.

--Guido van Rossum (home page: http://www.python.org/~guido/)



Mon, 20 Sep 1999 03:00:00 GMT  
 I'm stumped on threads

Subject:      I'm stumped on threads

Quote:
> Can anyone give me hint about what's wrong with the following program?
...
>def One_Thing(counter):
>    identone = threadone.get_ident()
>    print "One thing" + counter + ': ' + identone
>    counter = counter + 1

...

1) One_Thing will have no observable side effects since counter
    is a local variable of the function.  You need to use "global"
    in the function.

2) Even if you fix this the program you wrote is nondeterministic
    because the "main thread" may "race ahead" of the other two
    in which case 0 for the counter is possible, but 1 or 2 might
    also be possible.  Ain't threads confusing?

  -- Aaron Watters
===
...he says "What you gonna do with your life?"
"Oh, Daddy dear, you know you're still number 1
But girls just wanna have fun!"   -- Cindi Lauper.



Mon, 20 Sep 1999 03:00:00 GMT  
 I'm stumped on threads

Quote:
> import thread

> def One_Thing(counter):
>    identone = threadone.get_ident()
>    print "One thing" + counter + ': ' + identone
>    counter = counter + 1

> def Two_Thing(counter):
>    identtwo = threadtwo.get_ident()
>    print "Two thing" + counter + ': ' + identtwo
>    counter = counter + 1

> counter = 1
> threadone = thread.start_new_thread(One_Thing, (counter,))
> threadtwo = thread.start_new_thread(Two_Thing, (counter,))
> print threadone, threadtwo
> print counter

> it outputs None, None and 1 which makes sense if the two functions
> aren't called.

I think what is happening here is that One_Thing and Two_Thing are
each raising an AttributeError - get_ident() is a method of the thread
module. (thread.start_new_thread returns None)

Try changing the first line of One_Thing and Two_Thing to:
    identone=thread.get_ident() # same for identtwo...

---------------------------------------------------
B. Lloyd,                   Digital Creations, L.C.

Software Engineer           v: (540) 371-6909



Mon, 20 Sep 1999 03:00:00 GMT  
 I'm stumped on threads

Quote:

> Can anyone give me hint about what's wrong with the following program?

> ... and various discussions followed...

Having just this minute managed to finally compile a threaded python
under linux, I've written a summary of this discussion ;)

import thread
from time import sleep

class counterClass:
        def __init__(self):
                self.count = 0
        def view(self):
                self.count = self.count + 1
                return count

def threadFun(name, counter):
        for x in range(10):
                print "Thing %s (%s)"%(name, counter.view())
                sleep(10)

counter = counterClass()
thread.start_new_thread(threadFun,("One", counter))
thread.start_new_thread(threadFun,("Two", counter))

--
John "more a dr suess guy than a monty python guy" Lehmann

ps I don't think that anyone mentioned that start_new_thread returns
None, regardless (of anything)



Tue, 21 Sep 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. [Fwd: Re: I'm stumped on threads]

2. thread mystery on Solaris; even Guido is stumped

3. An AWK poser that's stumped a novice

4. Okay, I'm stumped.

5. error 19 - i'm stumped - snip [1/1]

6. I'm Stumped ...

7. Problems installing pythonwin - I'm stumped

8. proc global problem - I'm completely stumped

9. I'm stumped, need help

10. expect u question - i'm stumped - completely...

11. yes i'm stumped

12. Thread#kill doesn't kill processes inside a thread

 

 
Powered by phpBB® Forum Software