在python中使用多处理时有没有办法结束所有进程?

问题描述 投票:0回答:1

我是Python新手,我想找到一种方法来看看当我得到我想要的结果并将其放入其中一个进程时是否可以立即结束所有进程。我尝试了很多东西,比如

sys.exit()
和其他东西,但它从来没有奏效。

这是我的代码

import requests
import multiprocessing
import time
start_time = time.time()
def one():
 
    global start_time
    for n in range(1000, 4000):
      answer = requests.post('https://www.guessthepin.com/prg.php', data={'guess': n})
      if "Holy Moley!" not in str(answer.text):
       print("The pin is not", n)
       continue
      print(f"THE PIN IS: {n}")
      end_time = time.time()
      print(f"Time taken: {(end_time - start_time) / 60} minutes")
      sys.exit()
def two():
    
    global start_time
    for n in range(4000, 7000):
      answer = requests.post('https://www.guessthepin.com/prg.php', data={'guess': n})
      if "Holy Moley!" not in str(answer.text):
       print("The pin is not", n)
       continue
      print(f"THE PIN IS: {n}")
      end_time = time.time()
      print(f"Time taken: {(end_time - start_time) / 60} minutes")
      sys.exit()

def three():
    
    global start_time
    for n in range(7000, 10000):
      answer = requests.post('https://www.guessthepin.com/prg.php', data={'guess': n})
      if "Holy Moley!" not in str(answer.text):
       print("The pin is not", n)
       continue
      print(f"THE PIN IS: {n}")
      end_time = time.time()
      print(f"Time taken: {(end_time - start_time) / 60} minutes")
      sys.exit()
def four():
    global start_time
    for n in range(0, 1000):
      zero_thousand = f'{n:04}'
      request = requests.post('https://www.guessthepin.com/prg.php', data={'guess': zero_thousand})
      if "Holy Moley!" not in str(request.text):
       print("The pin is not", zero_thousand)
       continue
      print(f"THE PIN IS: {n}")
      end_time = time.time()
      print(f"Time taken: {(end_time - start_time) / 60} minutes")
      sys.exit()
if __name__ == "__main__":
    start_time = time.time()
    p1 = multiprocessing.Process(target=one)
    p2 = multiprocessing.Process(target=two)
    p3 = multiprocessing.Process(target=three)
    p4 = multiprocessing.Process(target=four)
    p1.start()
    p2.start()
    p3.start()
    p4.start()
    p1.join()
    p2.join()
    p3.join()
    p4.join()
    print("Done!")
    end_time = time.time()
    print(f"Time taken: {(end_time - start_time)/60} minutes")



所以在这里我想向网站发送请求,检查 pin 是否正确并继续。尽管当我使用多重处理并获得正确的引脚时请求部分可以工作,但它不会退出所有进程,而是会继续继续,直到验证所有数字。

python request multiprocessing
1个回答
0
投票

顺便说一句,看看你的函数

one
two
等,似乎可以使用多线程而不是多处理。

我认为最简单的解决方案是使用由

multiprocessing.pool.Pool(4)
创建的大小为 4 的多处理池。然后,您可以向池提交要完成的 4 个“任务”,指定任务完成时要调用的“回调”。第一个完成的任务将调用池上的terminate,这将终止其他提交的任务。
我还建议每个任务执行

start_time = time.time()

而不是继承全局

start_time
(在任何情况下都不需要
global start_time
语句),因为每个任务将在不同的时间开始。而且您不应该打电话给
sys.exit()
。您还应该阅读
PEP 8 – Python 代码风格指南
import time import requests USE_THEADING = False if USE_THREADING: from multiprocessing.dummy import Pool else: from multiprocessing.pool import Pool def one(): start_time = time.time() for n in range(1000, 4000): answer = requests.post('https://www.guessthepin.com/prg.php', data={'guess': n}) if "Holy Moley!" not in str(answer.text): print("The pin is not", n) continue print(f"THE PIN IS: {n}") end_time = time.time() print(f"Time taken: {(end_time - start_time) / 60} minutes") break def two(start_time): start_time = time.time() for n in range(4000, 7000): answer = requests.post('https://www.guessthepin.com/prg.php', data={'guess': n}) if "Holy Moley!" not in str(answer.text): print("The pin is not", n) continue print(f"THE PIN IS: {n}") end_time = time.time() print(f"Time taken: {(end_time - start_time) / 60} minutes") break def three(start_time): start_time = time.time() for n in range(7000, 10000): answer = requests.post('https://www.guessthepin.com/prg.php', data={'guess': n}) if "Holy Moley!" not in str(answer.text): print("The pin is not", n) continue print(f"THE PIN IS: {n}") end_time = time.time() print(f"Time taken: {(end_time - start_time) / 60} minutes") break def four(start_time): start_time = time.time() for n in range(0, 1000): zero_thousand = f'{n:04}' request = requests.post('https://www.guessthepin.com/prg.php', data={'guess': zero_thousand}) if "Holy Moley!" not in str(request.text): print("The pin is not", zero_thousand) continue print(f"THE PIN IS: {n}") end_time = time.time() print(f"Time taken: {(end_time - start_time) / 60} minutes") break if __name__ == "__main__": start_time = time.time() pool = Pool(4) def task_completion(result): print('result =', result) pool.terminate() pool.apply_async(one, callback=task_completion) pool.apply_async(two, callback=task_completion) pool.apply_async(three, callback=task_completion) pool.apply_async(four, callback=task_completion) # Wait for submitted tasks to complete pool.close() pool.join() print("Done!") end_time = time.time() print(f"Time taken: {(end_time - start_time)/60} minutes")

	
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.