problem on waiting exit thread and write on file

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

    #1

    problem on waiting exit thread and write on file

    I have a list of parameters.
    I need to run in thread a command, one thread for one parameter.
    So i made a for loop, creating 5 threads and waiting their stop with:
    for parameter in parameters

    Thread{
    ....
    write on BW2
    ....
    }

    Main {
    open file BW2 for write
    ..... (creating list of thread with a counter)....
    Stopped=False
    while (Stopped == False):
    if not thread5.isAlive ():
    if not thread4.isAlive ():
    if not thread3.isAlive ():
    if not thread2.isAlive ():
    if not thread1.isAlive ():
    Stopped=True
    if (Stopped == False):
    time.sleep(0.3)
    ......
    close file BW2
    }

    Somethimes i get however the error that i can't write on a file
    already closed
    There is a way more easy to wait that all children exit and to run a
    queue of threads?
    I tried also this:
    a=0
    while (a == 0):
    try:
    os.waitpid(-2, 0)
    except OSError, exc:
    # all CHILD finished
    a=1
    but the same problem persist. The only way to don't have error is to
    had a time.sleep(4) before closing the file from the main program.

  • Marc 'BlackJack' Rintsch

    #2
    Re: problem on waiting exit thread and write on file

    In <1181749731.260 357.192390@q19g 2000prn.googleg roups.com>, Flyzone wrote:
    I need to run in thread a command, one thread for one parameter.
    So i made a for loop, creating 5 threads and waiting their stop with:
    Main {
    open file BW2 for write
    ..... (creating list of thread with a counter)....
    Stopped=False
    while (Stopped == False):
    if not thread5.isAlive ():
    if not thread4.isAlive ():
    if not thread3.isAlive ():
    if not thread2.isAlive ():
    if not thread1.isAlive ():
    Stopped=True
    if (Stopped == False):
    time.sleep(0.3)
    If you create a list with threads where do the names `thread1` to
    `thread5` come from? This snippet expects the threads in a list or
    another iterable:

    for thread in threads:
    thread.join()

    Much shorter, isn't it!? :-)

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Flyzone

      #3
      Re: problem on waiting exit thread and write on file

      Marc 'BlackJack' Rintsch ha scritto:
      for thread in threads:
      thread.join()
      >
      Much shorter, isn't it!? :-)
      If i do like you tell, all thread will run together right? So i'll get
      troubles!
      I have a list of hostname, i run a network program and then i need to
      write some of the output that give me this command to a file.
      If i run the thread together, they can get problems writing on the
      same file.
      So I have splitted the list in 5 groups, the group 1 have filetmp 1,
      group 2 have filetmp 2 and so on...
      Then I start one thread for all groups, so 5 threads running together,
      the other waiting the end of one of them.

      Comment

      Working...