need some help with threading module...

chahnaz.ourzikene chahnaz.ourzikene at wanadoo.fr
Sun Dec 26 15:59:00 EST 2004


Hi all,

This is the first i post in this newsgroup, i hope my english is not too
bad...
Let's get straight to the point ! I have a little probleme using threads in
my little training example :
I wish to create two threads in my application, one thread (the subject)
will increment a variable, and another thread (the controller) will print a
message saying "i am waiting..." or someting like that, until the counter of
the first thread reaches 10 or higher. It will query the first thread object
about the value of that counter in a while loop until that counter reaches
10 or higher...

Here is the code i wrote (it doesn't work):

from threading import *

####################################

class Subject(object):


def __init__(self) :

self.counter = 0

t = Timer(0.00001,self.doIteratingStuff)

t.start()


def incrementCounter(self,n=1) :

self.counter = self.counter + n

def doIteratingStuff(self) :

for i in range(10) :

self.incrementCounter(4)

print "the counter is now", self.counter


def count(self):

return self.counter

####################################

class Controller(object):

def __init__(self, objectToControl) :

self.controlled = objectToControl

self.th = Thread(target = self.stopCounting)

def start(self):

self.th.start()


def stopCounting(self):

i = 0

#while(1):

for i in range(100000) :

print "###############################> waiting... ", i

i = i + 1

if self.controlled.count() >= 10 :

print "it is ten"

return

####################################

class Application (object) :

def __init__(self) :

pass

def launch(self) :

self.sub = Subject()

self.control = Controller(self.sub)

self.control.start()


####################################


a = Application()

a.launch()

Of course, the indentations are lost since i copied this code from my emacs
on linux, and then paste it on my outlook express (i don't have the net on
linux :(...).

The probleme with the code is that the controler thread never stops !!
that's why the while loop is now commented and replaced by a for loop... I
wish the problem is clear for all and that somebody can give some help !



Thanks for all and merry x-mas and blablablah.

Yacine Chaouche, France.





More information about the Python-list mailing list