multithreading-problem
Manish Jethani
manish.j at gmx.net
Sun Jul 27 16:14:21 EDT 2003
Diez B. Roggisch wrote:
> Hi,
>
> I expirienced strange problems when using the module threading:
>
> class Bar:
> def __init__(_, arg):
> _.arg = arg
> def work(_):
> while 1:
> print _.arg
>
> def foo(arg):
> b = Bar(arg)
> b.work()
>
> for i in xrange(4):
> thread = threading.Thread(target=lambda : foo(i))
> thread.start()
>
> What I'd expect would be a sequence of prints like this
> 1
> 2
> 3
> 4
> 1
> 3
> 4
> 2
>
> What I actualy get is this:
> 1
> 4
> 4
> 4
> 4
> 1
> 4
> 4
> 4
>
> Placing a time.sleep(3) after the thread.start() fixed things. So it appears
> that the curried lambda passed as target is somehow a reference equal for
> all four invocations.
No, I don't believe it to be true. It's probably just that the
other threads aren't getting a chance to run. Consider
time.sleep(0) in the while loop in work()
-Manish
--
Manish Jethani (manish.j at gmx.net)
phone (work) +91-80-51073488
More information about the Python-list
mailing list