problem on waiting exit thread and write on file

F

Flyzone

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.
 
M

Marc 'BlackJack' Rintsch

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
 
F

Flyzone

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top