I’m trying to find a way to make the following algorithm being processed on multiple cores, but I don’t get on a good point. Using a locked iterator shared between multiple processes wouldn’t be the most efficient way I think.
def sortCharset(set):
_set = ""
for c in set:
if c not in _set:
_set += c
set = _set
del _set
set = list(set)
set.sort()
return "".join(set)
def stringForInt(num, set, length):
setLen = len(set)
string = ""
string += set[num % setLen]
for n in xrange(1,length):
num //= setLen
string += set[num % setLen]
return string
def bruteforce(set, length, raw = False):
if raw is False:
set = sortCharset(set)
for n in xrange(len(set) ** length):
yield stringForInt(n, set, length)
Short explanation:
The code is used to create every possible combination
from a set of chars, i.e. to hack a password.
(Of course not my intention, just some Py-training. 😉
What is a good way to run this algorithm on multiple cores ?
The question isn’t really about naming style or how to get a sorted set of characters out of a string.
You might want to look into the multiprocessing module. I’m pretty much a n00b w/r/t multi-core parallelism but got something working:
The nature of the hack is that you need to use pickleable objects for the functions in
multiprocessingand only functions that are defined at the top-level are can be pickled. (There would be other ways around this usingmultiprocessing.Valueormultiprocessing.Managerbut they aren’t really worth going into for present purposes.)Here’s output for various runs: