import hashlib from threading import Thread, Semaphore import sys MAX_THREADS = 10 BUFFER_SIZE = 1024*512 class MD5File(Thread): def __init__(self, filename, lock): Thread.__init__(self) self.filename = filename self.md5 = None self.lock = lock def run(self): hash = hashlib.md5() with open(self.filename, 'rb') as fp: while True: data = fp.read(BUFFER_SIZE) if not len(data): break hash.update(data) print("%s %s" % (hash.hexdigest(), self.filename)) self.lock.release() class Program: def __init__(self): self.threads = [] self.number_lock = Semaphore(MAX_THREADS) def main(self): for filename in sys.argv[1:]: self.number_lock.acquire() thread = MD5File(filename, self.number_lock) thread.start() self.threads.append(thread) for thread in self.threads: thread.join() if __name__ == "__main__": Program().main()