thread problem

Tim Peters tim_one at email.msn.com
Mon Feb 28 20:43:34 EST 2000


[posted & mailed]

[Greg Wilson]
> I'm trying to implement futures in Python using threads.

Cool!

> ...
> My implementation is included below.  The problem is that I'm setting
> 'self.result' in the child thread, but when the __call()__ returns, the
> value has gone back to 'None'.  There's nothing in the docs about objects
> being cloned during thread creation --- I'd be grateful if anyone
> could tell me what's up.

It's trivial <wink>.  evaluate() *does* set self.result, but look at how you
invoke it:

>         self.result = self.evaluate()

Your evaluate() method doesn't have a "return" stmt, it just falls off the
end.  So evaluate() returns None, which you immediately write over the
correct self.result.  Change the line above to

    self.evaluate()

or change evalute() to return the result (I like that better, btw), and it
works fine.

BTW, think about using the threading module instead, and using a
threading.Event object.  It's much less error-prone than using the thread
module and a raw mutex.

you-don't-get-extra-points-for-skinning-the-beast-with-your-teeth<wink>-ly
    y'rs  - tim






More information about the Python-list mailing list