开发者

Synchronize a cleanup process between threads

I am planning a Python thread based project. Each thread needs to run an external program using subprocess.Popen and collect some output.

As a side affect,the external program may leave:

  • Temp files
  • Zombie Process

I can't run this cleanup for each thread, cause I don't have a way to make the association of a threads to its external program PIDs (this program launches another program) and it's temp files.

I am looking for the best way to clean up the temp files + kill the zombie process (I can find it by name but not by PID) after all of the threads has finished one iteration, and just before the next iteration will start.

I am aware that this kind of cle开发者_StackOverflow中文版anup will block all threads till the last one will finish, but I can live with this delay.

Can you please supply a short code snippet to demonstrate such a cleanup ?


OK, you've really got two things to look out for:

  • The cleanup mustn't start until the worker threads have finished a round of iteration
  • The worker threads mustn't start the next iteration until the cleanup is complete.

I think the best way to do this is to give each worker thread a pair of events (threading.Event()), readycleanup and cleanupfinished. Then in the worker threads:

# ... main operation
self.readycleanup.set() # Still within loop
self.cleanupfinished.wait()
self.cleanupfinished.clear()
# End of loop

Meanwhile, in the cleanup thread:

for worker in workerthreads:
    worker.readycleanup.wait()
do_cleanup_stuff()
for worker in workerthreads:
    worker.readycleanup.clear()
    worker.cleanupfinished.set()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜