Python multiprocessing

Asked 2 years ago, Updated 2 years ago, 95 views

Is there a way to kill all the child processes at that moment and measure the time when you find the correct answer in this program?

import multiprocessing
import time
import sys

start_time = time.time()

def bruteForce(st):
    for j in range (48,137):
        for k in range (48,137):
            for l in range (48,137):
                passWord = (chr(st)+chr(j)+chr(k)+chr(l))
                if passWord == ans :
                    print ("answer :", passWord)
                    return 0

def control(st):
    if bruteForce(st)==0:
        return 0
num_list = []
ans = 'A7uD'

for i in range(48,137):
    num_list.append(i)

if __name__ == '__main__' :
    pool = multiprocessing.Pool(processes=89)
    pool.map(control, num_list)
    pool.close()
    pool.join()
print("--- %s seconds ---" % (time.time() - start_time))

python multiprocessing

2022-09-22 11:13

1 Answers

import multiprocessing
import time
import sys

def bruteForce(st):
    for j in range (48,137):
        for k in range (48,137):
            for l in range (48,137):
                passWord = (chr(st)+chr(j)+chr(k)+chr(l))
                if passWord == ans :
                    print ("answer :", passWord)
                    return 0

def control(st):
    if bruteForce(st)==0:
        return 0

ans = 'A7uD'

if __name__ == '__main__' :
    start_time = time.time()
    pool = multiprocessing.Pool(processes=89)

    def finished(self):
        print("--- %s seconds ---" % (time.time() - start_time))
        pool.terminate()

    pool.map_async(control, range(48,137), callback=finished)
    pool.close()
    pool.join()

I think you can do it like this.


2022-09-22 11:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.