Вы пытались создать подмножество itertools.product
, а затем передать каждое подмножество работнику:
from multiprocessing import Pool
def crack(alist):
...
return result
def gather(result):
...
if __name__ == '__main__':
pool = Pool()
g = itertools.product(allchar, repeat = n)
while True:
try:
ss = [next(g) for _ in _ in range(10000)]
pool.apply_async(crack, ss, gather)
except StopIteration:
pool.apply_async(crack, ss, gather)
break
РЕДАКТИРОВАТЬ на @ juanpa.arrivillaga и изменить с: https://stackoverflow.com/a/43079667/2718295
Установите несколько очередей и заполните их в главном процессе. Работник несет ответственность за использование паролей:
from multiprocessing import Queue, Pool, Lock
def crack(q, q2, lock):
while True:
temp = q.get()
if temp is None:
break
hash_list = q2.get()
if not hash_list:
# empty set
break
h = hashlib.md5(temp.encode('utf-8')).hexdigest()
for hashvalue in hash_list:
if h == hashvalue:
hash_list = hash_list.discard(hashvalue)
with lock:
print(''.join(temp))
# resynchronize the hashlist
hash_list2 = q2.get()
q2.put(hash_list & hash_list2 )
if __name__ == '__main__':
NCORE = 4
q = Queue(maxsize=NCORE)
q2 = Queue(maxsize=NCORE)
lock = Lock()
hash_list = set([hash1,hash2,hash3,...])
allchar = string.ascii_letters + string.digits + string.punctuation
pool = Pool(NCORE, initializer=crack, initargs=(q,q2,lock))
q2.put(hash_list)
for result in itertools.product(allchar, repeat = n):
q.put(result)
for _ in range(NCORE):
q.put(None)
pool.close()
pool.join()