need some help with threading module...

2004-12-26 Thread chahnaz.ourzikene
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.1,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(10) :

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.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need some help with threading module...

2004-12-26 Thread chahnaz.ourzikene
I think it is more suitable in this form...



from threading import *



class Subject(object):


def __init__(self) :

self.counter = 0

t = Timer(0.1,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(10) :

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()



Thanls for all.



Yacine Chaouche, France.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need some help with threading module...

2004-12-27 Thread chahnaz.ourzikene
Hi everybody,

"Daniel Bickett" <[EMAIL PROTECTED]> a écrit dans le message de news:
[EMAIL PROTECTED]
> Instead of having the Controller query the Subject (not exactly
> plausible), I had it wait for a signal (threading.Event) as set by the
> Subject. You could also have it query a queue, as that is a generally
> accepted and thread-safe object to use, but for this purpose I chose an
> event and a global variable. (I'm aware that some would look down on
> this, but I didn't see a problem, as it was only modified by one thread
> amd printed by the other.)
>
> Daniel Bickett

Your approache is a little different, since the subject is responsible of
emiting a signal/event to the controller. I wish to avoid this situation.
The reason is that i want to write an application where the model (the
system) is completely unaware of the view and the controller. This way, i
could easily change the way the model and the view interact, while the code
of the model remains unchanged. I don't know if this is possible, that's why
i'm trying to solve this problem with threads ! and hope someone could help
me :)

Thanks to all.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need some help with threading module...

2004-12-27 Thread chahnaz.ourzikene

"M.E.Farmer" <[EMAIL PROTECTED]> a écrit dans le message de news:
[EMAIL PROTECTED]

> Just a warning!
> Threads and newbies don't mix well,
> many pitfalls and hard to find bugs await you.
> I would avoid using threads if at all possible.

Indeed :). But how will i learn using threads if i avoid using them :) ??

> Now we have all that over lets see some code.
>
> py> import threading
> py> class Test(threading.Thread):
> ...def run(self):
> ... x = 0
> ... while x < 10:
> ... print x
> ... x += 1
> ... print " Ending"
> Use it like this:
> py> Test().start()

Your example is interristing...somehow this is not exactly what i want to
realise. I have exposed the problem in an earlier reply...

Thank you all.

Yacine Chaouche -- France.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need some help with threading module...

2004-12-30 Thread chahnaz.ourzikene
Hi,

I fixed the code, it runs under Linux but not under windows 0_o ??! i guess
windows and Linux do not handle threads the same way.

However, i don't have the result i excpect.

Please have a look here :

## In this little program, i'm trying to find a way to yield data from a
thread within another
## thread, where both threads are NON-COOPERATIVE.
## To do so, i imagined an application launching two threads : a subject (to
yield data from)
## and a controller that will try to yield data from the subject. The
controller has a link
## with the subject, so he can call his methods and ask him to return the
desired data.
##
## The problem is that it appears that on windows systems, this program
idles and crashes after
## some seconds. On linux systems, this program runs but not the way i
figured : the two threads
## seem not to run in parallel...
##
## Look at the controller code : his job is to call the subject's methode
"count()" and compare
## the returned data to a threshold he's exepcting to reach. If the count
method of the subject
## returns a number less than threshold, than the controller loop again (in
a recursive function)
## until the subject's count method returns a number greater or equal to
threshold or the recursive funtion
## runs "limit" times ( to avoid unlimited recursive calls ).
##
## Now look at the subject code : his only job is to increment a counter in
a recursive methode.
## This counter is retrieved within the controller code via the count()
methode of the subject.
## When that counter reaches "threshold" (defined in the controller), the
controller returns.
##
## Execute the following code to see the result. You'll find someting like
this under linux :
##
##> controller waiting...  0 loops
##> controller waiting...  1 loops
##> controller waiting...  2 loops
##> controller waiting...  3 loops
##> controller waiting...  4 loops
##> controller waiting...  5 loops
##> controller waiting...  6 loops
##> controller waiting...  7 loops
##> controller waiting...  8 loops
##> controller waiting...  9 loops
##> controller waiting...  10 loops
##> controller waiting...  11 loops
##> controller waiting...  12 loops
##> controller waiting...  13 loops
##> controller waiting...  14 loops
##> controller waiting...  15 loops
##> controller waiting...  16 loops
##> controller waiting...  17 loops
##> controller waiting...  18 loops
##> controller waiting...  19 loops
##> controller waiting...  20 loops
##> controller waiting...  21 loops
##> controller waiting...  22 loops
##> controller waiting...  23 loops
##> controller waiting...  24 loops
##> controller waiting...  25 loops
##> controller waiting...  26 loops
##> controller waiting...  27 loops
##> controller waiting...  28 loops
##> controller waiting...  29 loops
##> controller waiting...  30 loops
##> controller waiting...  31 loops
##> controller waiting...  32 loops
##> controller waiting...  33 loops
##> controller waiting...  34 loops
##> controller waiting...  35 loops
##> controller waiting...  36 loops
##> controller waiting...  37 loops
##> controller waiting...  38 loops
##> controller waiting...  39 loops
##> controller waiting...  40 loops
##> controller waiting...  41 loops
##> controller waiting...  42 loops
##> controller waiting...  43 loops
##> controller waiting...  44 loops
##> controller waiting...  45 loops
##> controller waiting...  46 loops
##> controller waiting...  47 loops
##> controller waiting...  48 loops
##> controller waiting...  49 loops
## controller : limit of recursive loops reached :
## threshold 20 never reached
## Subject : the counter is now 0
## Subject : the counter is now 1
## Subject : the counter is now 2
## Subject : the counter is now 3
## Subject : the counter is now 4
## Subject : the counter is now 5
## Subject : the counter is now 6
## Subject : the counter is now 7
## Subject : the counter is now 8
## Subject : the counter is now 9
## Subject : the counter is now 10
## Subject : the counter is now 11
## Subject : the counter is now 12
## Subject : the counter is now 13
## Subject : the counter is now 14
## Subject : the counter is now 15
## Subject : the counter is now 16
## Subject : the counter is now 17
## Subject : the counter is now 18
## Subject : the counter is now 19
## Subject : the counter is now 20
## Subject : the counter is now 21
## Subject : the counter is now 22
## Subject : the counter is now 23
## Subject : the counter is now 24
## Subject : the counter is now 25
## Subject : the counter is now 26
## Subject : the counter is now 27
## Subject : the counter is now 28
## Subject : the counter is now 29
## Subject : the counter is now 30
## Subject : the counter is now 31
## Subject : the counter is now 32
## Subject : the counter is now 33
## Subject : the counter is now 34
##

Re: need some help with threading module...

2004-12-30 Thread chahnaz.ourzikene

"M.E.Farmer" <[EMAIL PROTECTED]> a écrit dans le message de news:
[EMAIL PROTECTED]

> What did you expect? This is what it did on win 2000/python 2.2.3
> ##> controller waiting...  0 loops
> ##> controller waiting...  1 loops
> Subject : the counter is now 0
> ##> controller waiting...  2 loops
> Subject : the counter is now 1
>
>
>

Yeah, looking to the trace i can easily say you have executed it from IDLE,
because IDLE is so slow that threads can have IRQs.
Try to execute it directly from a shell under windows, and you will see that
the Subject thread has much more CPU time thant the Controller thread.
Because Subject does his job in a loop, and have no sleep call inside its
code. Controller sleeps a while to let Subject run a little on the CPU.

Now try to run it under Linux directly from the shell and you'll have much
more time for Subject and very less for Controler. If you run it from IDLE
it can be a little more parallel.

Now there's a strange behaviour on my machine when i run my script within
IDLE under Windows : IDLE idles and never stops runnig !! i ran the same
script from a shell and it's all perfect...

Very strange..

Yacine Chaouche -- France

> It seems to be what you were trying to do.
>
> M.E.Farmer
>


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need some help with threading module...

2004-12-30 Thread chahnaz.ourzikene

"Steve Holden" <[EMAIL PROTECTED]> a écrit dans le message de news:
[EMAIL PROTECTED]

> Could be the OP is using Cygwin, which won't support threading by
> default and will give very confusing results
>
> just-a-guess-ly y'rs  - steve

Nice try :), but nope :).

Yacine Chaouche -- France.


-- 
http://mail.python.org/mailman/listinfo/python-list