0

I wanna get all the characters combinations (same position, Upper and Lower case alternation only) from a determined word then get a hash from each one in a background process. My code (oficial, here is only a practical example) is working very well, creating threads for do the sha256 hash aside. In production i wanna populate an array/list with at least every 200 word combinations to start a threaded function and deal with these data.

My Example code:

from itertools import product

def get_sha256(x):
  hash = hashlib.sha256(x().encode('utf-8')).hexdigest()
  print(hash)

result = product("aA","bB","cC","dD","eE","fF","hH")
for permutation in result:
  thread = get_sha256(''.join(permutation))
  thread.start()

In the code above we are starting a new thread for each combination but in a large cartesian product it will crash even the biggest server. I need to feed a presized array/list then start the background thread after N values and not for each every value. Is possible to do something like "for permutation[100] in result:" to do the loop from 100 to 100 values?

Is desirable to control the threads defining some "max thread" and maybe a semaphore to stop the loop and wait for some thread to continue feeding the presized data blocks to the sha256 function.

Python 3.7

James Z
  • 12,209
  • 10
  • 24
  • 44
  • To be clear, you just want to get the first 100 (or some other number) of values generated by `itertools.product`? – Karl Knechtel Apr 07 '20 at 04:24
  • Use a thread pool. Maybe check out [`concurrent.futures`](https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor) – juanpa.arrivillaga Apr 07 '20 at 04:52
  • @KarlKnechtel, thank you! I need to get 100 by 100 (for example) till the end. Not one by one as a normal "for" loop. – Marcelo Silva Apr 07 '20 at 14:47
  • @juanpa.arrivillaga, thank you! Do you think this method can "break" the loop until a new thread slot is available to start a new thread and run the loop for the next position? – Marcelo Silva Apr 07 '20 at 14:52
  • "I need to get 100 by 100 (for example) till the end. Not one by one as a normal "for" loop." Does https://stackoverflow.com/a/312644/523612 help? – Karl Knechtel Apr 08 '20 at 15:33
  • @KarlKnechtel perfect! This exactly i'm searching for. Thank you so much. Sorry for the delay. – Marcelo Silva Apr 21 '20 at 15:36

0 Answers0