我有一个系数为数字的函数.这取决于一些随机条件.
所以我要做的是在这个函数中运行多个处理器,首先找到因子的处理器返回值,所有处理器终止.
到目前为止,我的看法是非常错误的.处理器没有终止,我也不知道如何获取函数返回的值
flag = False
def rho(n, processor):
while True:
x = random.randrange(1, n-1)
x2 = x
gcd = 1
c = random.randrange(1, n-1)
while gcd == 1:
x = (x**2 + c) % n
x2 = (x2**2 + c) % n
x2 = (x2**2 + c) % n
gcd = math.gcd(abs(x - x2), n)
if gcd != n:
flag = True
print("Factor was found from "+process+" and is ", gcd)
return gcd
if __name__ == "__main__":
p1 = multiprocessing.Process(target=rho, args=(91, "process 1" ))
p2 = multiprocessing.Process(target=rho, args=(91, "process 2"))
p1.start()
p2.start()
if flag:
p1.terminate()
p2.terminate()
输出为:
Factor was found from process 2 and is 13
Factor was found from process 1 and is 7