need some help with threading module...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chahnaz.ourzikene

    #1

    need some help with threading module...

    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,s elf.doIterating Stuff)

    t.start()


    def incrementCounte r(self,n=1) :

    self.counter = self.counter + n

    def doIteratingStuf f(self) :

    for i in range(10) :

    self.incrementC ounter(4)

    print "the counter is now", self.counter


    def count(self):

    return self.counter

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

    class Controller(obje ct):

    def __init__(self, objectToControl ) :

    self.controlled = objectToControl

    self.th = Thread(target = self.stopCounti ng)

    def start(self):

    self.th.start()


    def stopCounting(se lf):

    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.st art()


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


    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.


  • M.E.Farmer

    #2
    Re: need some help with threading module...

    > Thanks for all and merry x-mas and blablablah
    There is no X in Christmas, and blablablah should read Happy New Year!
    [color=blue]
    >Of course, the indentations are lost since i copied this code from my[/color]
    emacs[color=blue]
    >on linux, and then paste it on my outlook express (i don't have the[/color]
    net on[color=blue]
    >linux :(...).[/color]

    I have had problems with whitespace being stripped before, but I just
    cannot understand WHY would you post KNOWING it was missing indents.
    Fix-it repost and maybe someone might help you.

    Hth,
    M.E.Farmer

    Comment

    • chahnaz.ourzikene

      #3
      Re: need some help with threading module...

      I think it is more suitable in this form...



      from threading import *

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

      class Subject(object) :


      def __init__(self) :

      self.counter = 0

      t = Timer(0.00001,s elf.doIterating Stuff)

      t.start()


      def incrementCounte r(self,n=1) :

      self.counter = self.counter + n

      def doIteratingStuf f(self) :

      for i in range(10) :

      self.incrementC ounter(4)

      print "the counter is now", self.counter


      def count(self):

      return self.counter

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

      class Controller(obje ct):

      def __init__(self, objectToControl ) :

      self.controlled = objectToControl

      self.th = Thread(target = self.stopCounti ng)

      def start(self):

      self.th.start()


      def stopCounting(se lf):

      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.st art()


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


      a = Application()

      a.launch()



      Thanls for all.



      Yacine Chaouche, France.


      Comment

      • M.E.Farmer

        #4
        Re: need some help with threading module...


        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.

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

        There are other ways to do this, but you need to do a web search on
        threading.py , there are many examples out there.
        hth,
        M.E.Farmer

        Comment

        • Daniel Bickett

          #5
          Re: need some help with threading module...

          I found your object-oriented approach, while admirable, a little
          muddled. So rather than modify your code, I simply took the paragraph
          you wrote describing the scenario and wrote my own.[1]

          Instead of having the Controller query the Subject (not exactly
          plausible), I had it wait for a signal (threading.Even t) 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

          NOTES:

          [1] Google killed my whitespace (as spaces _and_ tabs...) in the
          previews, so I pasted it on Nopaste:


          Comment

          • chahnaz.ourzikene

            #6
            Re: need some help with threading module...

            Hi everybody,

            "Daniel Bickett" <dbickett@gmail .com> a écrit dans le message de news:
            1104105568.2127 95.151000@f14g2 00...legr oups.com...[color=blue]
            > Instead of having the Controller query the Subject (not exactly
            > plausible), I had it wait for a signal (threading.Even t) 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[/color]

            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.


            Comment

            • chahnaz.ourzikene

              #7
              Re: need some help with threading module...


              "M.E.Farmer " <mefjr75@hotmai l.com> a écrit dans le message de news:
              1104102769.3219 23.4140@z14g200 0c...legrou ps.com...
              [color=blue]
              > 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.[/color]

              Indeed :). But how will i learn using threads if i avoid using them :) ??
              [color=blue]
              > 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()[/color]

              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.


              Comment

              • M.E.Farmer

                #8
                Re: need some help with threading module...

                chahnaz.ourzike ne wrote:[color=blue]
                > "M.E.Farmer " <mefjr75@hotmai l.com> a écrit dans le message de news:
                > 1104102769.3219 23.4140@z14g200 0c...legrou ps.com...
                >[color=green]
                > > 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.[/color]
                >
                > Indeed :). But how will i learn using threads if i avoid using them[/color]
                :) ??
                That was the point , you probably don't need them, and if so avoid
                them.
                If you want to learn how to use them , go for it!
                That is an excellent reason to mess with them, it was just a warning
                because you WILL be bitten by them, just a matter of time ;)
                [color=blue][color=green]
                > > 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()[/color]
                >
                > Your example is interristing... somehow this is not exactly what i[/color]
                want to[color=blue]
                > realise. I have exposed the problem in an earlier reply...
                > Yacine Chaouche -- France.[/color]

                I should have mentioned this was an attempt to show you how to make a
                thread 'exit' after it reached 10. You are having problems with the
                controller class, please look again at what I gave you this as an
                example. Did you run it?
                It was specific to your question. You were having problems making the
                controller exit.
                It should explain where you are lost, notice I inherit from Thread and
                not object, notice a much simpler way to write the thread ....
                You also mention the MVC pattern. If you are trying to implement MVC
                with threads you are going about it the wrong way ;)
                Hth,
                M.E.Farmer

                Comment

                • chahnaz.ourzikene

                  #9
                  Re: need some help with threading module...

                  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
                  ## Subject : the counter is now 35
                  ## Subject : the counter is now 36
                  ## Subject : the counter is now 37
                  ## Subject : the counter is now 38
                  ## Subject : the counter is now 39

                  from threading import Thread
                  ############### ############### ######
                  class Subject(Thread) :

                  def __init__(self,l oops = 100) :
                  Thread.__init__ (self)
                  self.counter = 0
                  self.recu_loops = 0
                  self.loops=loop s

                  def run(self):
                  self.doRecursiv eStuff(self.loo ps)

                  def incrementCounte r(self,n=1) :
                  self.counter = self.counter + n

                  def doIteratingStuf f(self, ntimes = 300) :
                  for i in xrange(ntimes) :
                  self.incrementC ounter()
                  print "Subject : the counter is now", self.counter

                  def doRecursiveStuf f(self,ntime = 80) :
                  if self.recu_loops < ntime :
                  print "Subject : the counter is now", self.counter
                  self.incrementC ounter()
                  self.recu_loops = self.recu_loops + 1
                  self.doRecursiv eStuff(ntime)
                  return

                  def count(self):
                  return self.counter
                  ############### ############### ######
                  class Controller(Thre ad):

                  def __init__(self, objectToControl , limit=100, threshold=20) :
                  Thread.__init__ (self)
                  self.controlled = objectToControl
                  self.recursive_ turns = 0
                  self.limit= limit
                  self.threshold = threshold

                  def run(self):
                  self.stopCounti ng()

                  def stopCounting(se lf):
                  if self.recursive_ turns < self.limit :
                  print "######> controller waiting... ", self.recursive_ turns,
                  "loops"
                  self.recursive_ turns = self.recursive_ turns + 1
                  if self.controlled .count() >= self.threshold :
                  print "controller : threshold", self.threshold, "reached"
                  return
                  self.stopCounti ng()
                  else :
                  print "controller : limit of recursive loops reached :"
                  print "threshold",sel f.threshold,"ne ver reached"
                  ############### ############### ######
                  class Application (object) :

                  def __init__(self) :
                  pass

                  def launch(self) :
                  self.sub = Subject(loops=4 0)
                  self.control = Controller(self .sub,limit=50,t hreshold=20)
                  self.control.st art()
                  self.sub.start( )
                  ############### ############### ######
                  a = Application()
                  a.launch()


                  Comment

                  • M.E.Farmer

                    #10
                    Re: need some help with threading module...

                    chahnaz.ourzike ne wrote:[color=blue]
                    > Hi,
                    >
                    > I fixed the code, it runs under Linux but not under windows 0_o ??! i[/color]
                    guess[color=blue]
                    > windows and Linux do not handle threads the same way.
                    >
                    > However, i don't have the result i excpect.[/color]
                    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
                    ######> controller waiting... 3 loops
                    Subject : the counter is now 2
                    ######> controller waiting... 4 loops
                    Subject : the counter is now 3
                    Subject : the counter is now 4 ######> controller waiting... 5 loops

                    Subject : the counter is now 5
                    ######> controller waiting... 6 loops
                    Subject : the counter is now 6
                    ######> controller waiting... 7 loops
                    Subject : the counter is now 7
                    Subject : the counter is now 8
                    ######> controller waiting... 8 loops
                    Subject : the counter is now ######> controller waiting... 9 loops
                    9
                    Subject : the counter is now 10
                    ######> controller waiting... 10 loops
                    Subject : the counter is now 11
                    Subject : the counter is now 12
                    ######> controller waiting... 11 loops
                    Subject : the counter is now 13
                    ######> controller waiting... 12 loops
                    Subject : the counter is now ######> controller waiting... 13 loops
                    14
                    Subject : the counter is now 15 ######> controller waiting... 14 loops

                    Subject : the counter is now 16
                    ######> controller waiting... 15 loops
                    Subject : the counter is now 17
                    ######> controller waiting... 16 loops
                    Subject : the counter is now 18
                    Subject : the counter is now 19
                    ######> controller waiting... 17 loops
                    Subject : the counter is now 20
                    controller : threshold 20 reached
                    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
                    Subject : the counter is now 35
                    Subject : the counter is now 36
                    Subject : the counter is now 37
                    Subject : the counter is now 38
                    Subject : the counter is now 39
                    It seems to be what you were trying to do.

                    M.E.Farmer

                    Comment

                    • Steve Holden

                      #11
                      Re: need some help with threading module...

                      M.E.Farmer wrote:
                      [color=blue]
                      > chahnaz.ourzike ne wrote:
                      >[color=green]
                      >>Hi,
                      >>
                      >>I fixed the code, it runs under Linux but not under windows 0_o ??! i[/color]
                      >
                      > guess
                      >[color=green]
                      >>windows and Linux do not handle threads the same way.
                      >>
                      >>However, i don't have the result i excpect.[/color]
                      >
                      > 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[/color]
                      [...][color=blue]
                      > Subject : the counter is now 38
                      > Subject : the counter is now 39
                      > It seems to be what you were trying to do.
                      >[/color]
                      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
                      --
                      Steve Holden http://www.holdenweb.com/
                      Python Web Programming http://pydish.holdenweb.com/
                      Holden Web LLC +1 703 861 4237 +1 800 494 3119

                      Comment

                      • M.E.Farmer

                        #12
                        Re: need some help with threading module...

                        Steve Holden wrote:
                        [snip][color=blue]
                        >Could be the OP is using Cygwin, which won't support threading by
                        >default and will give very confusing results[/color]

                        Thanks Steve,
                        Well your guess was better then mine :)
                        I didn't know Cygwin did not support threads by default , I will have
                        to remember that.
                        Why do you suppose he is having problems with this on Linux?
                        I am not on a Linux box to test it.
                        M.E.Farmer

                        Comment

                        • Steve Holden

                          #13
                          Re: need some help with threading module...

                          M.E.Farmer wrote:[color=blue]
                          > Steve Holden wrote:
                          > [snip]
                          >[color=green]
                          >>Could be the OP is using Cygwin, which won't support threading by
                          >>default and will give very confusing results[/color]
                          >
                          >
                          > Thanks Steve,
                          > Well your guess was better then mine :)
                          > I didn't know Cygwin did not support threads by default , I will have
                          > to remember that.
                          > Why do you suppose he is having problems with this on Linux?
                          > I am not on a Linux box to test it.
                          > M.E.Farmer
                          >[/color]
                          I believe he said it runs under Linux but not under Windows. Since you
                          (?) showed it does work under Windows, I guessed Cygwin.

                          regards
                          Steve
                          --
                          Steve Holden http://www.holdenweb.com/
                          Python Web Programming http://pydish.holdenweb.com/
                          Holden Web LLC +1 703 861 4237 +1 800 494 3119

                          Comment

                          • chahnaz.ourzikene

                            #14
                            Re: need some help with threading module...


                            "M.E.Farmer " <mefjr75@hotmai l.com> a écrit dans le message de news:
                            1104431262.4571 72.295520@c13g2 00...legr oups.com...
                            [color=blue]
                            > 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
                            >....
                            >....
                            >....[/color]

                            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
                            [color=blue]
                            > It seems to be what you were trying to do.
                            >
                            > M.E.Farmer
                            >[/color]


                            Comment

                            • chahnaz.ourzikene

                              #15
                              Re: need some help with threading module...


                              "Steve Holden" <steve@holdenwe b.com> a écrit dans le message de news:
                              unYAd.64262$Jk5 .31978@lakeread 01...
                              [color=blue]
                              > 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[/color]

                              Nice try :), but nope :).

                              Yacine Chaouche -- France.


                              Comment

                              Working...