Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当第一个进程在python3中完成时,如何终止并行进程?_Python_Python 3.x_Process_Multiprocessing_Terminate - Fatal编程技术网

当第一个进程在python3中完成时,如何终止并行进程?

当第一个进程在python3中完成时,如何终止并行进程?,python,python-3.x,process,multiprocessing,terminate,Python,Python 3.x,Process,Multiprocessing,Terminate,问题:当流程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)&quo

问题:当流程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的执行时间(两个函数都在并行进程中),该怎么办。因此,一旦第一个进程停止,我希望另一个进程停止。我该怎么做?我希望现在能清楚地描述这一点。再次为混乱感到抱歉

您是否尝试了
过程。是否终止

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()
我建议你们利用图书馆。它包装了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()

您不需要while循环-等待
p1
完成正是
p1.join()
为您所做的-它在等待时不会占用CPU。您能澄清您的问题吗?您可以在
p1
完成后直接执行
p2
,即
p2.terminate()
p1.join()
之后。阅读Hi!是的,我做了,但是再次检查我的编辑。很抱歉没有解释清楚。