我正在尝试解决python中的挖掘问题。给定一个字符串s
和一个整数z
我必须找到最少的n
,以便sha256(sha256(x))
以z
个零结尾,其中x
是通过将n
附加到s
给出的字符串。我写了以下代码:
from hashlib import sha256
from multiprocessing import Pool
def solve(string, zeros, cores):
with Pool(cores) as p:
for i in range(cores):
result = p.apply_async(sub_solve, args=(string, zeros, i, cores), callback = p.terminate)
return result
def sub_solve(s, z, n0, cores):
n = n0 - 1
d = ""
while d[:-z] != "0"*z:
n += cores
s1 = (s + str(n)).encode()
h1 = sha256(s1)
h2 = sha256(h1.digest())
d = h2.hexdigest()
if n % 100000 == 0:
print("%d: %s" %(n,d))
return n
使用string = s
调用solve
,zeros = z
并cores = number of cores to use
它应该在不同的内核中执行并行sub_solve
调用,每个内核都应该解决不同n
的问题。当其中一个工作进程解决问题时,整个池应终止工作。 当我运行solve
时,我得到以下输出:
>>> pow.solve("asd",2,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:UsersuserDesktoppow.py", line 7, in solve
result = p.apply_async(sub_solve, args=(string, zeros, i, cores), callback = p.terminate)
File "C:UsersuserAppDataLocalProgramsPythonPython36-32libmultiprocessingpool.py", line 355, in apply_async
raise ValueError("Pool not running")
ValueError: Pool not running
如何解决问题?
第一次迭代后,池将因回调而终止。因此,在下一次迭代中,没有正在运行的池。要解决此问题,您必须先运行循环,然后使用with
语句。
即 将with
语句与循环交换for
如下所示:
from hashlib import sha256
from multiprocessing import Pool
def solve(string, zeros, cores):
for i in range(cores):
with Pool(cores) as p:
result = p.apply_async(sub_solve, args=(string, zeros, i, cores), callback = p.terminate)
return result
def sub_solve(s, z, n0, cores):
n = n0 - 1
d = ""
while d[:-z] != "0"*z:
n += cores
s1 = (s + str(n)).encode()
h1 = sha256(s1)
h2 = sha256(h1.digest())
d = h2.hexdigest()
if n % 100000 == 0:
print("%d: %s" %(n,d))
return n
from hashlib import sha256
import multiprocessing
def solve(string, zeros, cores):
with multiprocessing.Pool(cores) as p:
for i in range(cores):
''' must call multiprocessing because the child process does Not know its p '''
result = p.apply_async(sub_solve, args=(string, zeros, i, cores), callback = multiprocessing.Pool().close())
return result
def sub_solve(s, z, n0, cores):
n = n0 - 1
d = ""
while d[:-z] != "0"*z:
n += cores
s1 = (s + str(n)).encode()
h1 = sha256(s1)
h2 = sha256(h1.digest())
d = h2.hexdigest()
if n % 100000 == 0:
print("%d: %s" %(n,d))
return n
if __name__ == "__main__":
print("sollving problem 1")
solve("asdfk",2,8)
print("solving probem 2")
solve("abcdefhijk",4,8)
输出:
sollving problem 1
100000: b982a515ed1f9da8d11be880cd621e13aec777abf3e08c78dc0849d7d3e591c9
200000: bb495b542d5e89f82a464fee84e2ad33f18de9ad2817b233d292b77ae42ff584
300000: cb6e9e02de1f2b76250c47b5d7e1121bfb90d32451a7d75bfb538df76b427ab1
400000: da6cec93d44719ec44925365090b2b49e79ada037b1a64608fd855e83e3a08af
500000: 15b43eaa0a500a337d04f37a01b616e5068effacb807e27e760b97aa58b68147
600000: a97fc82597b7b80b1b2c29bbbeddea3eb93a8690728e596b29eef8aba02a2ec8
700000: 8647001bb6d7ba352e2cc24ed31a1bc858812ed864a208256c4f20078509a52d
800000: 08d55a3b590cba473f7391915824a38ac4c1012aebf29d0aad3d0ea5c0654c2d
900000: 8ca55f8e9585a7212ca494370f30738c2ba8ef2bc7fde6d8d182dbb079ecca0f
1000000: 7362508f0d1e3b0da1e6250dba8fee831d94dd9bfe2837935750f0deb10a1a08
solving probem 2
100000: 99dfca2809f20a173657d7f767573641b263a2a233d062001e4e979d944919d8
200000: 1d2a7ab78930756300a0061aa01489045ee2a51c4987c7364f6410811e102db1
300000: b326e26dcdd28c212880fe0dd83dcc9d41d9053bcad7c92263177c370d1131b7
400000: adbb9c6d8acaf680739f8b5e8c86efd68a9a2c7e62d54531123298e4329c2764
500000: f77358148f8dea09533111044b75032e45b77579f4fd567a23ad06b8c6f8d29a
600000: 1324e1d8e2883fe5b91c91e1a65d26218fb7c08b37c2804d2c904082d516a5e7
700000: 50cc0e97b1b91bad942d36c9f3c549978db4ecb666ea08ab9b9a20012ff2c14a
800000: b98e254395f26fbe4e60857f3cffe12a3751c991f705fb8f6b3853ac9aa20b13
900000: 1ea083e7c135040d60eb4b681b1aeb73425384b532e3ab2110f608b9db6b6c38
1000000: 89e8b62ce9ddfc12fb40ee1bfe82028a08c5aabb2184434128ff03b820e0c104