我有以下python3代码:
from multiprocessing import Pool, cpu_count
import hashlib
import signal
def checkHash(argsList):
key = argsList[0]
pattern = argsList[1]
cpusNum = argsList[2]
coreId = argsList[3]
while True:
coreId += cpusNum
newHash = hashlib.sha256()
newFeed = key + '_' + str(coreId)
newHash.update(newFeed.encode('utf-8'))
result = newHash.hexdigest()
if result.startswith(pattern):
return coreId
def signalIgnorer():
signal.signal(signal.SIGINT, signal.SIG_IGN)
class POWGenerator:
def __init__(self, key, difficulty, requestedCores):
self.key = key
self.beginningPattern = '0' * difficulty
coresNum = cpu_count()
if requestedCores <= coresNum:
self.coresToUse = requestedCores
else:
self.coresToUse = coresNum
def getSolution(self):
jobsArgsList = []
for cou in range(0,self.coresToUse):
jobsArgsList.append((self.key, self.beginningPattern, self.coresToUse, cou))
with Pool(self.coresToUse, signalIgnorer) as workPool:
try:
for gotmatch in workPool.imap_unordered(checkHash, jobsArgsList):
if gotmatch:
return gotmatch
except KeyboardInterrupt:
return None
if __name__ == "__main__":
import sys
key = sys.argv[1]
difficulty = sys.argv[2]
cores = sys.argv[3]
gen = POWGenerator(key, int(difficulty), int(cores))
print("Solution: {}".format(gen.getSolution()))
它用于每次为gpg公钥指纹创建工作证明。
键:字符串(gpg公钥指纹)
难度:正整数(sha256sum应该启动多少个零)
核心:正整数(使用多少核心)
它需要一个数字(解决方案)并在每次检查时从0开始给它一个值,直到:sha256sum(指纹+'_'+解决方案)给出一个以难度<开头的哈希值/ em>零个数。
我想要的是能够中断程序(即键盘中断)并在以后重新开始而不从头开始搜索。最简单的方法是在键盘中断上创建一个json文件,并在启动程序时首先搜索特定文件以从中加载数据。
但我不知道该怎么做。我不知道如何从每个sigle进程获取coreId
变量以及键盘中断的进程名称,并将它们保存为json文件中的键[Value],以便在下次程序运行时使用它。你有什么想法吗?