diff options
author | zyp <zyp@localhost> | 2006-05-16 15:57:39 +0200 |
---|---|---|
committer | zyp <zyp@localhost> | 2006-05-16 15:57:39 +0200 |
commit | 8e9e23cddd72d2dd407164f8a7e835fa6c84b4d2 (patch) | |
tree | 9cfae322a7c94b502ceb981c4fb5cade26d05252 /pyanidb | |
parent | ea36dd2639b7b562aee9aa587489be9bf9bc4c73 (diff) |
[project @ zyp-20060516135739-925e0fe0819b13d6]
[project @ 32]
Replaced inline hash-code with hash-module.
Diffstat (limited to 'pyanidb')
-rw-r--r-- | pyanidb/hash.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/pyanidb/hash.py b/pyanidb/hash.py new file mode 100644 index 0000000..35c05ae --- /dev/null +++ b/pyanidb/hash.py @@ -0,0 +1,40 @@ +import multihash, threading, time + +def file_hash(name, algorithms): + h = multihash.Multihash(*algorithms) + f = open(name) + data = f.read(32768) + while data: + h.update(data) + data = f.read(32768) + f.close() + return h + +class Hashthread(threading.Thread): + def __init__(self, filelist, hashlist, algorithms, *args, **kwargs): + self.filelist = filelist + self.hashlist = hashlist + self.algorithms = algorithms + threading.Thread.__init__(self, *args, **kwargs) + def run(self): + try: + while 1: + f = self.filelist.pop(0) + h = file_hash(f, self.algorithms) + self.hashlist.append((f, h)) + except IndexError: + return + +def hash_files(files, num_threads = 1, algorithms = ('ed2k',)): + hashlist = [] + threads = [] + for x in xrange(num_threads): + thread = Hashthread(files, hashlist, algorithms) + thread.start() + threads.append(thread) + while hashlist or sum([thread.isAlive() for thread in threads]): + try: + yield hashlist.pop(0) + except IndexError: + time.sleep(0.1) + raise StopIteration |