Newbie having issues with threads

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

    Newbie having issues with threads

    I'm a newbie trying to write a script that uses threads. I'm right
    now a little bit stuck in understanding why the code snippet I wrote
    doesn't seem to be entering the function defined in the
    start_new_threa d() call.

    If I run it as is (the threaded version), the output is:

    UA_1 configuring...
    UA_1 halting..

    But if I comment out the line w/ the thread and just call the function
    directly, everything seems to work OK:

    UA_1 configuring...
    UA_1 executing...
    UA_1 halting...

    Can anyone tell me why the thread doesn't seem to invoke the function
    "execute()" ? I'm running Python 2.4.3.

    Here is my code:

    ===========
    import thread

    class Test(object):
    def __init__(self, instanceID):
    self.instanceID = instanceID
    def configure(self) :
    print self.instanceID + " configuring..."
    def execute(self):
    print self.instanceID + " executing..."
    def halt(self):
    print self.instanceID + " halting..."

    if __name__ == "__main__":
    """usage: sipp_auto [options]"""

    ua1 = Test("UA_1")

    ua1.configure()

    #ua1.execute()
    thread.start_ne w_thread(ua1.ex ecute, ())

    ua1.halt()

    ===========
    Thanks, James
  • James Calivar

    #2
    Re: Newbie having issues with threads

    Well, that seemed to do the trick. Thanks for the tip! I guess as a
    novice and having no investment in the older "thread" module, I'll
    just use the Threading module from now on.

    James
    =====

    PS Here is my new code snippet:
    =====

    #!/usr/bin/python

    import threading

    class Test(object):
    def __init__(self, instanceID):
    self.instanceID = instanceID
    def configure(self) :
    print self.instanceID + " configuring..."
    def execute(self):
    print self.instanceID + " executing..."
    def halt(self):
    print self.instanceID + " halting..."

    class testThread(thre ading.Thread):
    def __init__(self, ID):
    self.ID = ID
    threading.Threa d.__init__(self )
    def run(self):
    ua1.execute()

    if __name__ == "__main__":
    """usage: sipp_auto [options]"""

    ua1 = Test("UA_1")

    ua1.configure()

    thread = testThread("UA_ 1")
    thread.start()
    thread.join()

    ua1.halt()

    Comment

    • Raja Baz

      #3
      Re: Newbie having issues with threads

      On Thu, 31 Jul 2008 14:09:12 -0700, James Calivar wrote:
      I'm a newbie trying to write a script that uses threads. I'm right now
      a little bit stuck in understanding why the code snippet I wrote doesn't
      seem to be entering the function defined in the start_new_threa d() call.
      >
      If I run it as is (the threaded version), the output is:
      >
      UA_1 configuring...
      UA_1 halting..
      >
      But if I comment out the line w/ the thread and just call the function
      directly, everything seems to work OK:
      >
      UA_1 configuring...
      UA_1 executing...
      UA_1 halting...
      >
      Can anyone tell me why the thread doesn't seem to invoke the function
      "execute()" ? I'm running Python 2.4.3.
      >
      Here is my code:
      >
      ===========
      import thread
      >
      class Test(object):
      def __init__(self, instanceID):
      self.instanceID = instanceID
      def configure(self) :
      print self.instanceID + " configuring..."
      def execute(self):
      print self.instanceID + " executing..."
      def halt(self):
      print self.instanceID + " halting..."
      >
      if __name__ == "__main__":
      """usage: sipp_auto [options]"""
      >
      ua1 = Test("UA_1")
      >
      ua1.configure()
      >
      #ua1.execute()
      thread.start_ne w_thread(ua1.ex ecute, ())
      >
      ua1.halt()
      >
      ===========
      Thanks, James
      I've run into this problem before. The main problem is that the thread
      started via start_new_threa d exits when the program does(instead of
      finishing its work first). So what happens here is that sometimes it may
      have the time to execute, and sometimes it won't

      changing:
      thread.start_ne w_thread(ua1.ex ecute, ())

      ua1.halt()

      to:
      thread.start_ne w_thread(ua1.ex ecute, ())
      from time import sleep
      sleep(0.01) # this is barely even noticeable

      ua1.halt()

      fixes the problem in this case and the output is correct.

      However, you seem to have taken up using the threading module which is
      probably better anyway, just wanted to explain why this was happening.

      Comment

      Working...