Do thread die?

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

    #1

    Do thread die?

    Hi,

    I just have a simple question about threads. My classes inherits from
    threading.Threa d class. I am calling threading.Threa d.run() method to
    spawn a few threads to parallel some parts of my program. No thread
    re-use, pooling, joining ... just plainly spawn a thread, run a routine.

    So, at the end of run(), what happens to the thread? Just die?

    While I am on it, can threading.Threa d.run() accept any parameters?

    My current implementation may be ugly. I have a class

    class myThread(thread ing.Thread):
    def __init__(self, func):
    self.func = func
    threading.Threa d.__init__(self )
    def run(self):
    print '%s function running' % self.func
    self.func()

    which is used in

    class myClass: #using myThread
    def __init__(self): pass
    def aFunc(self): pass
    def bFunc(self): pass
    def runAll(self):
    myThread(self.a Func).start()
    myThread(self.b Func).start()

    if __name__=='__ma in__': myClass().runAl l()

    Is this a good way?

    Thanks and cheers
    Maurice
  • Sybren Stuvel

    #2
    Re: Do thread die?

    Maurice LING enlightened us with:[color=blue]
    > So, at the end of run(), what happens to the thread? Just die?[/color]

    Yep.
    [color=blue]
    > While I am on it, can threading.Threa d.run() accept any parameters?[/color]

    Nope. Pass them to the constructor and remember them.
    [color=blue]
    > class myThread(thread ing.Thread):
    > def __init__(self, func):
    > self.func = func
    > threading.Threa d.__init__(self )
    > def run(self):
    > print '%s function running' % self.func
    > self.func()[/color]

    You don't need to do this, since you can pass a callable object to the
    Thread constructor. Read the first lines of
    http://docs.python.org/lib/thread-objects.html again. That would
    change your code to:

    class myClass:
    def __init__(self): pass
    def aFunc(self): pass
    def bFunc(self): pass

    def runAll(self):
    threading.Threa d(self.aFunc).s tart()
    threading.Threa d(self.bFunc).s tart()

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa

    Comment

    • Bryan Olson

      #3
      Re: Do thread die?

      Maurice LING wrote:[color=blue]
      > Hi,
      >
      > I just have a simple question about threads. My classes inherits from
      > threading.Threa d class. I am calling threading.Threa d.run() method to
      > spawn a few threads to parallel some parts of my program. No thread
      > re-use, pooling, joining ... just plainly spawn a thread, run a routine.
      >
      > So, at the end of run(), what happens to the thread? Just die?[/color]

      Just die, mostly. It may do a bit of clean-up, setting its
      affairs in order, arraning for burial/cremation/organ-donation
      and such, to avoid leaving any problem for posterity.


      Incidentally, the "threading" module, with its "Thread" class,
      has one major advantage over the "thread" module: the "setDaemon"
      method of class Thread.

      [color=blue]
      > While I am on it, can threading.Threa d.run() accept any parameters?[/color]

      Not unless you override it; but you can pass parameters when
      constructing the Thread, like this:

      t = Thread(target=w ork_to_do, args=(67, 'Bill Johnston'), kwargs=some_dic t)

      t.start() will then execute the default Thread.run(), which
      will execute work_to_do with two positional arguments -- 67 and
      'Bill Johston' -- and whatever key-word arguments were in some_dict.

      If you override 'Thread.run', you can define your override to take
      whatever parameters you want.

      [color=blue]
      > My current implementation may be ugly. I have a class
      >
      > class myThread(thread ing.Thread):
      > def __init__(self, func):
      > self.func = func
      > threading.Threa d.__init__(self )
      > def run(self):
      > print '%s function running' % self.func
      > self.func()[/color]

      [...]
      [color=blue]
      > Is this a good way?[/color]

      I don't see anything wrong with it, though to me, it seems a
      little heavy. To run your func, all you need to do is:

      Thread(target=f unc).start()

      Though nine times out of ten, you'd want:

      t = Thread(target=f unc, args=(arg1, arg2, and_so_on))
      t.setDaemon(Tru e)
      t.start()

      If you really need that "print", you could build it into your
      'target' function; or you could subclass Thread more generally,
      so your subclass adds the print and behaves exactly the same
      otherwise. I regard debugging as important, but I do not
      believe that this particular print warrants the general
      inclusion implied.


      Other Pythoners have disagree with me on various matters at
      issue here. In particular, others have disagreed with my
      advocacy of multiple lines of execution.


      --
      --Bryan

      Comment

      • Steve Horsley

        #4
        Re: Do thread die?

        Maurice LING wrote:[color=blue]
        > Hi,
        >
        > I just have a simple question about threads. My classes inherits from
        > threading.Threa d class. I am calling threading.Threa d.run() method to
        > spawn a few threads to parallel some parts of my program. No thread
        > re-use, pooling, joining ... just plainly spawn a thread, run a routine.
        >
        > So, at the end of run(), what happens to the thread? Just die?[/color]

        As far as you are concerned, yes. They really just return to
        wherever they came from. How they get created before they call
        the run() method, and where they go after run() returns is down
        to the implementation of the interpreter. Your code never sees
        them before or after.[color=blue]
        >
        > While I am on it, can threading.Threa d.run() accept any parameters?
        >[/color]
        No. But you can override it in a subclass.
        [color=blue]
        > My current implementation may be ugly. I have a class
        >
        > class myThread(thread ing.Thread):
        > def __init__(self, func):
        > self.func = func
        > threading.Threa d.__init__(self )
        > def run(self):
        > print '%s function running' % self.func
        > self.func()
        >
        > which is used in
        >
        > class myClass: #using myThread
        > def __init__(self): pass
        > def aFunc(self): pass
        > def bFunc(self): pass
        > def runAll(self):
        > myThread(self.a Func).start()
        > myThread(self.b Func).start()[/color]


        There is a school of thought that says that a derived object
        should never be altered such that it cannot be used as its parent
        could. I happen to agree largely with this. Now, your MyThread
        implementation is _reducing_ the functionality of its ancestor
        Thread object such that you could not use a myThread in place of
        a Thread. I believe that you should not be subclassing Thread to
        do this. Your myClass doesn't need it anyway. Look at this
        modified myClass:

        class myClass2:
        def aFunc(self): pass
        def bFunc(self): pass
        def runAll(self):
        threading.Threa d(target=self.a Func).start()
        threading.Threa d(target=self.b Func).start()


        Steve

        Comment

        • Maurice LING

          #5
          Re: Do thread die?

          [color=blue]
          >[color=green]
          >> My current implementation may be ugly. I have a class
          >>
          >> class myThread(thread ing.Thread):
          >> def __init__(self, func):
          >> self.func = func
          >> threading.Threa d.__init__(self )
          >> def run(self):
          >> print '%s function running' % self.func
          >> self.func()
          >>
          >> which is used in
          >>
          >> class myClass: #using myThread
          >> def __init__(self): pass
          >> def aFunc(self): pass
          >> def bFunc(self): pass
          >> def runAll(self):
          >> myThread(self.a Func).start()
          >> myThread(self.b Func).start()[/color]
          >
          >
          >
          > There is a school of thought that says that a derived object should
          > never be altered such that it cannot be used as its parent could. I
          > happen to agree largely with this. Now, your MyThread implementation is
          > _reducing_ the functionality of its ancestor Thread object such that you
          > could not use a myThread in place of a Thread. I believe that you should
          > not be subclassing Thread to do this. Your myClass doesn't need it
          > anyway. Look at this modified myClass:
          >
          > class myClass2:
          > def aFunc(self): pass
          > def bFunc(self): pass
          > def runAll(self):
          > threading.Threa d(target=self.a Func).start()
          > threading.Threa d(target=self.b Func).start()
          >[/color]
          Thanks everyone. Furthering that, is the following legal?

          class myClass3:
          def aFunc(self, a): pass
          def bFunc(self, b): pass
          def runAll(self, a, b):
          threading.Threa d(target=self.a Func, args = (a)).start()
          threading.Threa d(target=self.b Func, args = (b)).start()

          I do have another dumb question which is OT here. Say aFunc method
          instantiates a SOAP server that serves forever, will it prevent bFunc
          from running as a separate thread?

          For example,

          class myClass4:
          def repeat(self, s): return s+s
          def aFunc(self, a):
          import SOAPpy
          serv = SOAPpy.SOAPServ er((a[0], a[1]))
          serv.registerFu nction(repeat)
          serv.serve_fore ver()
          def bFunc(self, b): pass
          def runAll(self, a, b):
          threading.Threa d(target=self.a Func, args = (a)).start()
          threading.Threa d(target=self.b Func, args = (b)).start()

          if __name__=='__ma in__': myClass4().runA ll(['localhost', 8000], 'hi')

          Will the 2nd thread (bFunc) ever run since the 1st thread is running
          forever? Intuitively, I think that both threads will run but I just want
          to be doubly sure, because some of my program logic depends on the 2nd
          thread running while the 1st thread acts as a SOAP server or something.

          Thanks and Cheers
          Maurice

          Comment

          • Frithiof Andreas Jensen

            #6
            Re: Do thread die?


            "Maurice LING" <mauriceling@ac m.org> wrote in message
            news:dgh54e$e8c $1@domitilla.ai oe.org...
            [color=blue]
            > I do have another dumb question which is OT here. Say aFunc method
            > instantiates a SOAP server that serves forever, will it prevent bFunc
            > from running as a separate thread?[/color]

            If the SOAP server thread never sleeps or block, it will effectively stop
            everything else in your program by eating all the CPU time available. If it
            does some IO and other OS functions, probably not because it is likely to
            block on those - I do not know SOAPpy in detail, but it being a socket-based
            server it should end up in a select loop somewhere. i.e. block when no work
            is available. which is what you want.
            [color=blue]
            > For example,
            >
            > class myClass4:
            > def repeat(self, s): return s+s
            > def aFunc(self, a):
            > import SOAPpy
            > serv = SOAPpy.SOAPServ er((a[0], a[1]))
            > serv.registerFu nction(repeat)
            > serv.serve_fore ver()
            > def bFunc(self, b): pass
            > def runAll(self, a, b):
            > threading.Threa d(target=self.a Func, args = (a)).start()
            > threading.Threa d(target=self.b Func, args = (b)).start()
            >
            > if __name__=='__ma in__': myClass4().runA ll(['localhost', 8000], 'hi')
            >
            > Will the 2nd thread (bFunc) ever run since the 1st thread is running
            > forever? Intuitively, I think that both threads will run but I just want
            > to be doubly sure, because some of my program logic depends on the 2nd
            > thread running while the 1st thread acts as a SOAP server or something.[/color]

            Both should run independently, sharing the CPU-time available for your
            application. Remember "main" is a thread too, so you will want "main" to
            hang around while your threads are running and you will want "main" to block
            on something also, thread.join(), time.sleep(), command line parser e.t.c.
            whatever is natural.


            Comment

            • Maurice LING

              #7
              Re: Do thread die?

              Frithiof Andreas Jensen wrote:
              [color=blue]
              > "Maurice LING" <mauriceling@ac m.org> wrote in message
              > news:dgh54e$e8c $1@domitilla.ai oe.org...
              >
              >[color=green]
              >>I do have another dumb question which is OT here. Say aFunc method
              >>instantiate s a SOAP server that serves forever, will it prevent bFunc
              >>from running as a separate thread?[/color]
              >
              >
              > If the SOAP server thread never sleeps or block, it will effectively stop
              > everything else in your program by eating all the CPU time available. If it
              > does some IO and other OS functions, probably not because it is likely to
              > block on those - I do not know SOAPpy in detail, but it being a socket-based
              > server it should end up in a select loop somewhere. i.e. block when no work
              > is available. which is what you want.
              >
              >[color=green]
              >>For example,
              >>
              >>class myClass4:
              >> def repeat(self, s): return s+s
              >> def aFunc(self, a):
              >> import SOAPpy
              >> serv = SOAPpy.SOAPServ er((a[0], a[1]))
              >> serv.registerFu nction(repeat)
              >> serv.serve_fore ver()
              >> def bFunc(self, b): pass
              >> def runAll(self, a, b):
              >> threading.Threa d(target=self.a Func, args = (a)).start()
              >> threading.Threa d(target=self.b Func, args = (b)).start()
              >>
              >>if __name__=='__ma in__': myClass4().runA ll(['localhost', 8000], 'hi')
              >>
              >>Will the 2nd thread (bFunc) ever run since the 1st thread is running
              >>forever? Intuitively, I think that both threads will run but I just want
              >>to be doubly sure, because some of my program logic depends on the 2nd
              >>thread running while the 1st thread acts as a SOAP server or something.[/color]
              >
              >
              > Both should run independently, sharing the CPU-time available for your
              > application. Remember "main" is a thread too, so you will want "main" to
              > hang around while your threads are running and you will want "main" to block
              > on something also, thread.join(), time.sleep(), command line parser e.t.c.
              > whatever is natural.
              >
              >[/color]

              Somehow I cannot reconcile your replies because I am essentially asking
              the same thing and expanding on the original question with an example of
              what I am trying to do, but the replies seems contradictory. Do you mind
              to explain a bit more?

              thanks
              Maurice

              Comment

              • Maurice LING

                #8
                Re: Do thread die?

                Frithiof Andreas Jensen wrote:
                [color=blue]
                > "Maurice LING" <mauriceling@ac m.org> wrote in message
                > news:dgh54e$e8c $1@domitilla.ai oe.org...
                >
                >[color=green]
                >>I do have another dumb question which is OT here. Say aFunc method
                >>instantiate s a SOAP server that serves forever, will it prevent bFunc
                >>from running as a separate thread?[/color]
                >
                >
                > If the SOAP server thread never sleeps or block, it will effectively stop
                > everything else in your program by eating all the CPU time available. If it
                > does some IO and other OS functions, probably not because it is likely to
                > block on those - I do not know SOAPpy in detail, but it being a socket-based
                > server it should end up in a select loop somewhere. i.e. block when no work
                > is available. which is what you want.
                >
                >[color=green]
                >>For example,
                >>
                >>class myClass4:
                >> def repeat(self, s): return s+s
                >> def aFunc(self, a):
                >> import SOAPpy
                >> serv = SOAPpy.SOAPServ er((a[0], a[1]))
                >> serv.registerFu nction(repeat)
                >> serv.serve_fore ver()
                >> def bFunc(self, b): pass
                >> def runAll(self, a, b):
                >> threading.Threa d(target=self.a Func, args = (a)).start()
                >> threading.Threa d(target=self.b Func, args = (b)).start()
                >>
                >>if __name__=='__ma in__': myClass4().runA ll(['localhost', 8000], 'hi')
                >>
                >>Will the 2nd thread (bFunc) ever run since the 1st thread is running
                >>forever? Intuitively, I think that both threads will run but I just want
                >>to be doubly sure, because some of my program logic depends on the 2nd
                >>thread running while the 1st thread acts as a SOAP server or something.[/color]
                >
                >
                > Both should run independently, sharing the CPU-time available for your
                > application. Remember "main" is a thread too, so you will want "main" to
                > hang around while your threads are running and you will want "main" to block
                > on something also, thread.join(), time.sleep(), command line parser e.t.c.
                > whatever is natural.
                >
                >[/color]

                Somehow I cannot reconcile your replies because I am essentially asking
                the same thing and expanding on the original question with an example of
                what I am trying to do, but the replies seems contradictory. Do you mind
                to explain a bit more?

                thanks
                Maurice

                Comment

                • Maurice LING

                  #9
                  Re: Do thread die?

                  Frithiof Andreas Jensen wrote:
                  [color=blue]
                  > "Maurice LING" <mauriceling@ac m.org> wrote in message
                  > news:dgh54e$e8c $1@domitilla.ai oe.org...
                  >
                  >[color=green]
                  >>I do have another dumb question which is OT here. Say aFunc method
                  >>instantiate s a SOAP server that serves forever, will it prevent bFunc
                  >>from running as a separate thread?[/color]
                  >
                  >
                  > If the SOAP server thread never sleeps or block, it will effectively stop
                  > everything else in your program by eating all the CPU time available. If it
                  > does some IO and other OS functions, probably not because it is likely to
                  > block on those - I do not know SOAPpy in detail, but it being a socket-based
                  > server it should end up in a select loop somewhere. i.e. block when no work
                  > is available. which is what you want.
                  >
                  >[color=green]
                  >>For example,
                  >>
                  >>class myClass4:
                  >> def repeat(self, s): return s+s
                  >> def aFunc(self, a):
                  >> import SOAPpy
                  >> serv = SOAPpy.SOAPServ er((a[0], a[1]))
                  >> serv.registerFu nction(repeat)
                  >> serv.serve_fore ver()
                  >> def bFunc(self, b): pass
                  >> def runAll(self, a, b):
                  >> threading.Threa d(target=self.a Func, args = (a)).start()
                  >> threading.Threa d(target=self.b Func, args = (b)).start()
                  >>
                  >>if __name__=='__ma in__': myClass4().runA ll(['localhost', 8000], 'hi')
                  >>
                  >>Will the 2nd thread (bFunc) ever run since the 1st thread is running
                  >>forever? Intuitively, I think that both threads will run but I just want
                  >>to be doubly sure, because some of my program logic depends on the 2nd
                  >>thread running while the 1st thread acts as a SOAP server or something.[/color]
                  >
                  >
                  > Both should run independently, sharing the CPU-time available for your
                  > application. Remember "main" is a thread too, so you will want "main" to
                  > hang around while your threads are running and you will want "main" to block
                  > on something also, thread.join(), time.sleep(), command line parser e.t.c.
                  > whatever is natural.
                  >
                  >[/color]

                  Somehow I cannot reconcile your replies because I am essentially asking
                  the same thing and expanding on the original question with an example of
                  what I am trying to do, but the replies seems contradictory. Do you mind
                  to explain a bit more?

                  thanks
                  Maurice

                  Comment

                  Working...