import os, subprocess import threading, Queue error = False # delete previous files for i in range(1000): name = 'file%04d.txt' % i try: os.remove(name) except OSError: pass # queue of pending tasks queue = Queue.Queue() # the worker threads def do_work(): while True: filename = queue.get() print "checking %s" % filename p = subprocess.Popen(['checkfile.exe', filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = p.communicate()[0].rstrip() if output[-2:]!="OK": error = True print output queue.task_done() for i in range(6): worker = threading.Thread(target=do_work) worker.setDaemon(True) worker.start() print "start" content = "dabale arroz a la zorra el abad\n" * 100 for i in range(1000): name = 'file%04d.txt' % i f = open(name, 'w') f.write(content) f.close() queue.put(name) print "waiting for all threads to finish" queue.join() if error: print "errors!" else: print "ok, no errors found"