multithreading-problem

Diez B. Roggisch deets_noospaam at web.de
Sun Jul 27 18:24:21 CEST 2003


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.

The used python is 2.2.<latest-cygwin>.

Regards,

Diez




More information about the Python-list mailing list