thread stomp?

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

    thread stomp?

    class1 (threading.Thre ad):
    def __init__(self, dir):
    self.dir = dir
    threading.Threa d.__init__(self )

    def run(self):
    do stuff in dir here...

    class2 (threading.Thre ad):
    def __init__(self, dir, dir2):
    self.file = g
    self.dir2 = dir2
    threading.Threa d.__init__(self )

    def run(self):
    do stuff in dir here...
    output to dir2

    class main
    for i in dircache.listdi r(dir):
    //run over files
    class1(dir).sta rt()

    for j in dircache.listdi r(dir2):
    //run over files, dependent on stuff in dir
    class2(dir2).st art()

    think just using the loops above enough to prevent class2 threads from
    starting before class1 threads done

    not true

    class2 threads seems to start prior to class1 threads done; class2
    results depend on class1 finish first

    any help?
  • D'Arcy J.M. Cain

    #2
    Re: thread stomp?

    On Mon, 12 May 2008 20:00:52 GMT
    pyn3wb <pyn3wb@pyn3wb. comwrote:
    class1 (threading.Thre ad):
    ^
    SyntaxError: invalid syntax

    Give us a script that works. Ideally it should output something that
    indicates what the error is.
    //run over files
    This is not a comment in Python.
    class2 threads seems to start prior to class1 threads done; class2
    results depend on class1 finish first
    What are you trying to do? If all you care about is making one thread
    depend on another finishing, why make two threads? Just have one
    thread do one thing and then do the other.

    Not every problem is a threading problem.

    --
    D'Arcy J.M. Cain <darcy@druid.ne t | Democracy is three wolves
    http://www.druid.net/darcy/ | and a sheep voting on
    +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

    Comment

    • Jerry Hill

      #3
      Re: thread stomp?

      On Mon, May 12, 2008 at 4:00 PM, pyn3wb <pyn3wb@pyn3wb. comwrote:
      class main
      Why are you making main a class? It should almost certainly be a
      function (defined with "def", not "class").
      for i in dircache.listdi r(dir):
      //run over files
      class1(dir).sta rt()
      You've started a bunch of threads. If you want to wait for them to
      all finish, you need to call .join() on them all. You can't do that
      unless you save references to your threads, like this:

      threads = []
      for i in dircache.listdi r(dir):
      new_thread = class1(dir)
      new_thread.star t()
      threads.append( new_thread)

      for thread in threads:
      thread.join()

      Beyond that, the code you've posted is not correct. You've left off a
      required ":" in your definition of class main, and your comments are
      not valid python comments. Your for loops don't make a whole lot of
      sense to me either. Are they supposed to be iterating across files in
      a directory? If so, why do you start up all of your threads with
      "dir" instead of "i" or "j" as the parameter to your new thread?

      --
      Jerry

      Comment

      Working...