【问题标题】:How do I terminate the parallel process when the first one has finished in python3?当第一个在 python3 中完成时,如何终止并行进程?
【发布时间】:2020-07-06 15:03:31
【问题描述】:

问题:如何在进程号 1 完成后停止进程号 2?

加长版:我编写了一个多进程 2 个函数的脚本,但我真的不知道如何在第一个进程完成后停止第二个进程。

我的代码:

def printHelloWorld(): # So this takes 5.0053 seconds to completly run
    print("Hello World.")
    time.sleep(5)
    print("Hello World. (after 5 seconds)")


def printText(): # And this takes 10.0102 seconds to completly run
    print("Some text.")
    time.sleep(10)
    print("Some text. (after 10 seconds)")


if __name__ == "__main__": # I multiprocessed these 2 functions to reduce time. 
    # But what I actually want is to terminate process 2 when process 1 has finished.
    p1 = Process(target = printHelloWorld) 
    p2 = Process(target = printText)
    p1.start()
    p2.start()          
    p1.join()
    p2.join()

我尝试了一个 while 循环来检查 p1.is_alive == False 何时应该终止进程 2,但它不起作用。我也搜索了答案,但没有找到符合我要求的答案。

感谢您阅读/回答!

让我澄清一些事情:如果我无法正确解释,我很抱歉,但我想问的是,我如何检查哪个进程首先完成并终止第二个进程,因为它不再需要?

示例:如果我们不知道函数 1 和 2 的执行时间怎么办(这两个函数都在一个并行进程中)。因此,一旦第一个进程停止,我希望另一个进程停止。我怎样才能做到这一点?我希望现在可以清楚地描述这一点。再次抱歉造成混乱!

【问题讨论】:

  • 您不需要 while 循环 - 等待 p1 完成正是 p1.join() 将为您做的事情 - 而且它不会忙于消耗 CPU 而它正在等待。
  • 你能澄清你的问题吗?在p1 完成之后,您可以字面上 terminate() p2 - 即p2.terminate()p1.join() 之后。

标签: python python-3.x process multiprocessing terminate


【解决方案1】:

我建议您使用 Pebble 库。它包装了 Python 的标准库线程和多处理对象。但它很有可能取消正在运行的通话

import time
from concurrent.futures import wait, FIRST_COMPLETED
from pebble import ProcessPool

def printHelloWorld():  # So this takes 5.0053 seconds to completly run
    print("Hello World.")
    time.sleep(5)
    print("Hello World. (after 5 seconds)")


def printText():  # And this takes 10.0102 seconds to completly run
    print("Some text.")
    time.sleep(10)
    print("Some text. (after 10 seconds)")


if __name__ == "__main__":  # I multiprocessed these 2 functions to reduce time.
    with ProcessPool(max_workers=2) as pool:
        f1 = pool.schedule(printHelloWorld)
        f2 = pool.schedule(printText)
        done, not_done = wait((f1, f2), return_when=FIRST_COMPLETED)
        for f in not_done:
            f.cancel()

【讨论】:

    【解决方案2】:

    你试过Process.terminate吗?

    import time
    from multiprocessing import Process
    
    
    def printHelloWorld(): # So this takes 5.0053 seconds to completly run
        print("Hello World.")
        time.sleep(5)
        print("Hello World. (after 5 seconds)")
    
    
    def printText(): # And this takes 10.0102 seconds to completly run
        print("Some text.")
        time.sleep(10)
        print("Some text. (after 10 seconds)")
    
    
    if __name__ == "__main__": # I multiprocessed these 2 functions to reduce time.
        # But what I actually want is to terminate process 2 when process 1 has finished.
        p1 = Process(target = printHelloWorld)
        p2 = Process(target = printText)
        p1.start()
        p2.start()
        p1.join()
        p2.terminate()  # terminate process 2
        p2.join()
    

    【讨论】:

    • 嗨!是的,我做了,但再次检查我的编辑。很抱歉没有清楚地解释它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多