Multithreading

Karl M. Syring syring at email.com
Fri Jan 25 05:02:55 EST 2002


"Geiger Ho" <s997659 at ee.cuhk.edu.hk> schrieb
> Hi all,
> 
>   I have a script below which intends to have output: 0, 1, 2, 3, 4.
> However what I get is 4, 4, 4, 4, 4. Why? How can I correct it?
> 
> ------------------------------------------
> from threading import Thread
> 
> class MyThread(Thread):
> def __init__(self, num_t):
> Thread.__init__(self)
> MyThread.num = num_t
> def run(self):
> print MyThread.num
> 
> for i in range(5)
> x = MyThread(i)
> x.start()
> ------------------------------------------

Uhm, that is broken in multiple ways. Try

#
from threading import Thread

class MyThread(Thread):
 def __init__(self, num_t):
  Thread.__init__(self)
  self.num = num_t
 def run(self):
  print self.num

for i in range(5):
 x = MyThread(i)
 x.start()
#

Karl M. Syring





More information about the Python-list mailing list