Question about thread

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

    #1

    Question about thread

    Refering to the following codes I found, there is nothing displayed in the
    console, may I ask why?

    def thrd(param): # the thread worker function
    print "Received",para m

    import thread
    for i in range(5): # start five threads passing i to each one
    thread.start_ne w_thread(thrd,( i,))

    Thanks in advance
  • Valkyrie

    #2
    Re: Question about thread

    To be more precise, what I want to do is to have a threaded program to handle
    some jobs concurrently (while those jobs are fetching information from the
    Internet, so threading could speed up the process if the network is slow)

    start
    | (blahblahblah.. .)
    v
    +-----+-----+-----+-----+
    | | | | |
    --+-- --+-- --+-- --+-- --+--
    | | | | | | | | | |
    | A | | B | | C | | D | | E |
    | | | | | | | | | |
    --+-- --+-- --+-- --+-- --+--
    | | | | |
    +-----+-----+-----+-----+
    | (blahblahblah.. .)
    v
    finish!

    While process A-E will access the shared variables within the same program, and
    each of those processes will have to parse a document on its own.


    Valkyrie wrote:[color=blue]
    > Refering to the following codes I found, there is nothing displayed in the
    > console, may I ask why?
    >
    > def thrd(param): # the thread worker function
    > print "Received",para m
    >
    > import thread
    > for i in range(5): # start five threads passing i to each one
    > thread.start_ne w_thread(thrd,( i,))
    >
    > Thanks in advance[/color]

    Comment

    • Russell Blau

      #3
      Re: Question about thread

      "Valkyrie" <valkyrie@cuhk. edu.hk> wrote in message
      news:1100875373 .911763@eng-ser6...[color=blue]
      > Refering to the following codes I found, there is nothing displayed in the
      > console, may I ask why?
      >
      > def thrd(param): # the thread worker function
      > print "Received",para m
      >
      > import thread
      > for i in range(5): # start five threads passing i to each one
      > thread.start_ne w_thread(thrd,( i,))[/color]

      You may ask, but when I tried your code, here is what happened:

      Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
      win32
      Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
      >>> def thrd(param): # the thread worker function[/color][/color][/color]
      .... print "Received",para m
      ....[color=blue][color=green][color=darkred]
      >>> import thread
      >>> for i in range(5): # start five threads passing i to each one[/color][/color][/color]
      .... thread.start_ne w_thread(thrd,( i,))
      ....
      1960
      836
      232
      2864
      3692[color=blue][color=green][color=darkred]
      >>> Received 0[/color][/color][/color]
      Received 1
      Received 2
      Received 3
      Received 4

      Note that the numbers 1960, etc., appear to be the return values of the
      thread.start_ne w_thread() function.


      --
      I don't actually read my hotmail account, but you can replace hotmail with
      excite if you really want to reach me.


      Comment

      • Valkyrie

        #4
        Re: Question about thread

        When I do it line by line in python's console, I have similar result to youl.
        But when I try to run it in a file, say:

        python demo.py

        It just returns me nothing. I have no idea on this right now...

        Russell Blau wrote:[color=blue]
        > "Valkyrie" <valkyrie@cuhk. edu.hk> wrote in message
        > news:1100875373 .911763@eng-ser6...
        >[color=green]
        >>Refering to the following codes I found, there is nothing displayed in the
        >>console, may I ask why?
        >>
        >>def thrd(param): # the thread worker function
        >> print "Received",para m
        >>
        >>import thread
        >>for i in range(5): # start five threads passing i to each one
        >> thread.start_ne w_thread(thrd,( i,))[/color]
        >
        >
        > You may ask, but when I tried your code, here is what happened:
        >
        > Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
        > win32
        > Type "help", "copyright" , "credits" or "license" for more information.
        >[color=green][color=darkred]
        >>>>def thrd(param): # the thread worker function[/color][/color]
        >
        > ... print "Received",para m
        > ...
        >[color=green][color=darkred]
        >>>>import thread
        >>>>for i in range(5): # start five threads passing i to each one[/color][/color]
        >
        > ... thread.start_ne w_thread(thrd,( i,))
        > ...
        > 1960
        > 836
        > 232
        > 2864
        > 3692
        >[color=green][color=darkred]
        >>>>Received 0[/color][/color]
        >
        > Received 1
        > Received 2
        > Received 3
        > Received 4
        >
        > Note that the numbers 1960, etc., appear to be the return values of the
        > thread.start_ne w_thread() function.
        >
        >[/color]

        Comment

        • Peter Hickman

          #5
          Re: Question about thread

          Valkyrie wrote:[color=blue]
          > Refering to the following codes I found, there is nothing displayed in the
          > console, may I ask why?
          >
          > def thrd(param): # the thread worker function
          > print "Received",para m
          >
          > import thread
          > for i in range(5): # start five threads passing i to each one
          > thread.start_ne w_thread(thrd,( i,))
          >
          > Thanks in advance[/color]

          The problem with your script is that it will run too fast before it quits and
          kills all the threads.

          if you add

          a = 0
          for i in range(100000):
          a = a + 1

          to the end you will see the output.

          Comment

          • Craig Ringer

            #6
            Re: Question about thread

            On Fri, 2004-11-19 at 23:45, Valkyrie wrote:[color=blue]
            > When I do it line by line in python's console, I have similar result to youl.
            > But when I try to run it in a file, say:
            >
            > python demo.py
            >
            > It just returns me nothing. I have no idea on this right now...[/color]

            You need to cause the main thread to wait on the child threads until
            they've all finished, otherwise - as Peter Hickman just mentioned - your
            script will terminate and all threads will end. Ten seconds on Google
            found this:

            http://www.faqts.com/knowledge_base/view.phtml/aid/1364

            --
            Craig Ringer

            Comment

            • Valkyrie

              #7
              Re: Question about thread

              Thank you for your advice. I have quite seriously searched for it in fact, but
              just without the luck on finding such article...

              Craig Ringer wrote:[color=blue]
              > On Fri, 2004-11-19 at 23:45, Valkyrie wrote:
              >[color=green]
              >>When I do it line by line in python's console, I have similar result to youl.
              >>But when I try to run it in a file, say:
              >>
              >>python demo.py
              >>
              >>It just returns me nothing. I have no idea on this right now...[/color]
              >
              >
              > You need to cause the main thread to wait on the child threads until
              > they've all finished, otherwise - as Peter Hickman just mentioned - your
              > script will terminate and all threads will end. Ten seconds on Google
              > found this:
              >
              > http://www.faqts.com/knowledge_base/view.phtml/aid/1364
              >
              > --
              > Craig Ringer
              >[/color]

              Comment

              • Scott David Daniels

                #8
                Re: Question about thread

                Valkyrie wrote:[color=blue]
                > Thank you for your advice. I have quite seriously searched for it in fact, but
                > just without the luck on finding such article...[/color]
                Can you suggest where this information might have shown up
                that you would have found it? If so, you may want to submit
                a documentation bug or patch request. That is, repay the
                group by making it more likely that the next person with your
                problem finds their answer on their own.

                --Scott David Daniels
                Scott.Daniels@A cm.Org

                Comment

                • Jarek Zgoda

                  #9
                  Re: Question about thread

                  Craig Ringer wrote:
                  [color=blue][color=green]
                  >>When I do it line by line in python's console, I have similar result to youl.
                  >>But when I try to run it in a file, say:
                  >>
                  >>python demo.py
                  >>
                  >>It just returns me nothing. I have no idea on this right now...[/color]
                  >
                  > You need to cause the main thread to wait on the child threads until
                  > they've all finished, otherwise - as Peter Hickman just mentioned - your
                  > script will terminate and all threads will end. Ten seconds on Google
                  > found this:
                  >
                  > http://www.faqts.com/knowledge_base/view.phtml/aid/1364[/color]

                  Are you sure? I keep observing that child threads continue its execution
                  until completion even if I do nothing in main thread. Consider this example:

                  import random, time
                  from threading import Thread

                  class Worker(Thread):

                  def __init__(self, name):
                  Thread.__init__ (self)
                  self.name = name
                  print 'thread %s created' % self.name

                  def run(self):
                  amt = random.randint( 1, 4)
                  print 'thread %s waiting %d seconds' % (self.name, amt)
                  time.sleep(amt)
                  print 'thread %s finished' % self.name

                  def __del__(self):
                  print 'thread %s is being destroyed' % self.name

                  if __name__ == '__main__':
                  for i in range(10):
                  t = Worker('%.04d' % (i + 1, ))
                  t.start()
                  print 'finished creating threads'

                  Even after printing "finished creating threads" all spawned threads
                  continue its execution up to final "thread nnnn is being destroyed", so
                  I think there's no need to take any special action (unless it's Python
                  on iSeries, buy this is another story) to wait for completion.

                  --
                  Jarek Zgoda
                  http://jpa.berlios.de/ | http://www.zgodowie.org/

                  Comment

                  • Diez B. Roggisch

                    #10
                    Re: Question about thread

                    >[color=blue]
                    > Even after printing "finished creating threads" all spawned threads
                    > continue its execution up to final "thread nnnn is being destroyed", so
                    > I think there's no need to take any special action (unless it's Python
                    > on iSeries, buy this is another story) to wait for completion.[/color]

                    The OP used the module "thread", while you used "threading" and didn't set
                    setDaemon(False ). Then python waits until all threads are finished.

                    From the thread module docs:


                    When the main thread exits, it is system defined whether the other threads
                    survive. On SGI IRIX using the native thread implementation, they survive.
                    On most other systems, they are killed without executing try ... finally
                    clauses or executing object destructors.

                    --
                    Regards,

                    Diez B. Roggisch

                    Comment

                    • Craig Ringer

                      #11
                      Re: Question about thread

                      On Sat, 2004-11-20 at 04:56, Jarek Zgoda wrote:
                      [color=blue]
                      > Are you sure? I keep observing that child threads continue its execution
                      > until completion even if I do nothing in main thread. Consider this example:[/color]

                      Well, I'm very far from an expert, but I've also experienced the
                      poster's reported behaviour even with quite simple programs. I'm
                      surprised to hear that the initially described behaviour isn't always
                      the case, and would be interested in knowing more about when it is, and
                      is not, generally encountered.

                      --
                      Craig Ringer

                      Comment

                      • Jarek Zgoda

                        #12
                        Re: Question about thread

                        Diez B. Roggisch wrote:
                        [color=blue][color=green]
                        >>Even after printing "finished creating threads" all spawned threads
                        >>continue its execution up to final "thread nnnn is being destroyed", so
                        >>I think there's no need to take any special action (unless it's Python
                        >>on iSeries, buy this is another story) to wait for completion.[/color]
                        >
                        > The OP used the module "thread", while you used "threading" and didn't set
                        > setDaemon(False ). Then python waits until all threads are finished.[/color]

                        Well... I was bit too fast in replying. ;)
                        [color=blue]
                        > From the thread module docs:
                        >
                        > When the main thread exits, it is system defined whether the other threads
                        > survive. On SGI IRIX using the native thread implementation, they survive.
                        > On most other systems, they are killed without executing try ... finally
                        > clauses or executing object destructors.[/color]

                        So it looks that using threading should be preferred over using thread,
                        as presented example works identically on Windows and on linux (it
                        doesn't work on AS/400, though...).

                        --
                        Jarek Zgoda
                        http://jpa.berlios.de/ | http://www.zgodowie.org/

                        Comment

                        • Peter Otten

                          #13
                          Re: Question about thread

                          Jarek Zgoda wrote:
                          [color=blue][color=green]
                          >> You need to cause the main thread to wait on the child threads until
                          >> they've all finished, otherwise - as Peter Hickman just mentioned - your
                          >> script will terminate and all threads will end. Ten seconds on Google
                          >> found this:
                          >>
                          >> http://www.faqts.com/knowledge_base/view.phtml/aid/1364[/color]
                          >
                          > Are you sure? I keep observing that child threads continue its execution
                          > until completion even if I do nothing in main thread. Consider this
                          > example:[/color]

                          You are using the real thing - the highlevel threading.Threa d class - not
                          the underlying thread.start_ne w_thread() function.
                          The threading module creates a _MainThread (a Thread subclass) instance that
                          registers itself as an atexit handler and waits for the other (non-demon)
                          Thread instances to terminate.

                          Failure-to-provide-the-source-code-will-ultimately-work-against
                          -companies-sitting-on-a-pile-of-crap-patents-ly yours

                          Peter



                          Comment

                          • Valkyrie

                            #14
                            Re: Question about thread

                            I just looked up for the keywords "python thread example" in google and few
                            useful links are retrieved. It seems that most of the examples are too complicated.

                            Scott David Daniels wrote:[color=blue]
                            > Valkyrie wrote:
                            >[color=green]
                            >>Thank you for your advice. I have quite seriously searched for it in fact, but
                            >>just without the luck on finding such article...[/color]
                            >
                            > Can you suggest where this information might have shown up
                            > that you would have found it? If so, you may want to submit
                            > a documentation bug or patch request. That is, repay the
                            > group by making it more likely that the next person with your
                            > problem finds their answer on their own.
                            >
                            > --Scott David Daniels
                            > Scott.Daniels@A cm.Org[/color]

                            Comment

                            • Valkyrie

                              #15
                              Re: Question about thread

                              So may I ask is threading do exactly the same in terms of functionalities as
                              those in thread?


                              Jarek Zgoda wrote:[color=blue]
                              > Diez B. Roggisch wrote:
                              >[color=green][color=darkred]
                              >>> Even after printing "finished creating threads" all spawned threads
                              >>> continue its execution up to final "thread nnnn is being destroyed", so
                              >>> I think there's no need to take any special action (unless it's Python
                              >>> on iSeries, buy this is another story) to wait for completion.[/color]
                              >>
                              >>
                              >> The OP used the module "thread", while you used "threading" and didn't
                              >> set
                              >> setDaemon(False ). Then python waits until all threads are finished.[/color]
                              >
                              >
                              > Well... I was bit too fast in replying. ;)
                              >[color=green]
                              >> From the thread module docs:
                              >>
                              >> When the main thread exits, it is system defined whether the other
                              >> threads
                              >> survive. On SGI IRIX using the native thread implementation, they
                              >> survive.
                              >> On most other systems, they are killed without executing try ... finally
                              >> clauses or executing object destructors.[/color]
                              >
                              >
                              > So it looks that using threading should be preferred over using thread,
                              > as presented example works identically on Windows and on linux (it
                              > doesn't work on AS/400, though...).
                              >[/color]

                              Comment

                              Working...