1
\$\begingroup\$

How can I do this in a more performant way? I'm concerned that throwing the data in a queue like this is defeating the purpose of yield from the open() and also how about handling the shared reference to file handle?

import hashlib
import sys
from threading import Thread
import queue


class HashWorker(Thread):

    def __init__(self, queue, fp_out):
        Thread.__init__(self)
        self.queue = queue
        self.fp_out = fp_out

    def run(self):
        while True:
            password = self.queue.get()
            try:
                hash_threaded(password, self.fp_out)
            finally:
                self.queue.task_done()


def main(argv):

    if sanitize(argv) is False:
        return -1

    # fp = open(argv[1], "r")
    fp_out = open("sha256hashes.txt", "a+")

    myqueue = queue.Queue()

    for x in range(8):
        worker = HashWorker(myqueue, fp_out)
        worker.daemon = True
        worker.start()

    for l in open(argv[1], "r"):
        myqueue.put(l)

    myqueue.join()
    print("Done.")
    fp_out.close()


def sanitize(argv):
    if len(argv) != 2:
        print("USAGE: {} filename.txt".format(argv[0]))
        return False
    return True


def hash_threaded(password, fp_out):
    line_bytes = bytes(password, "ascii")
    output_line = hashlib.sha256(line_bytes).hexdigest()
    fp_out.write("{}\n".format(output_line))
    #  print(".", end='')


if __name__ == '__main__':
    main(sys.argv)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.