简体   繁体   English

如何杀死所有的多处理?

[英]How to kill all the multiprocessing?

I have a python function which calls another function in a multiprocessing.How can I kill all the multiprocessing using python?我有一个 python 函数,它在多处理中调用另一个函数。如何使用 python 终止所有多处理?

This is the outer function which is also a multiprocess这是外部函数,它也是一个多进程

p = Process(target=api.queue_processor, args=(process_queue_in, process_queue_out, process_lock,
                                          command_queue_in, command_queue_out, api.OBJECTIVE_0_5NA, quit_event,
                                          log_file))
p.start()

This is the function which is getting called这是被调用的函数

def queue_processor(process_queue_in, process_queue_out, process_lock,command_queue_in, command_queue_out, objective_type_NA, quit_event,log_file=None):
  
                    slide, roi_coordinates = process_obj[0]
                    logger.info("Received for imaging {} with ROI {} and type {}".format(slide.tile_dir,
                                                                                         roi_coordinates,
                                                                                         slide.slide_type))

                    p = Process(target=iad.image_slide,
                                args=(slide.tile_dir, slide.slide_type, roi_coordinates, exception_queue,
                                      objective_type_NA, logger, clog_path))
                    p.start() #process to kill

I want to kill the second multiprocess.(commented)我想杀死第二个多进程。(评论)

We can kill or terminate a process immediately by using the terminate() method.我们可以使用 terminate() 方法立即终止或终止进程。 We will use this method to terminate the child process, which has been created with the help of function, immediately before completing its execution.我们将使用此方法在完成执行之前立即终止在函数的帮助下创建的子进程。

import multiprocessing
import time
def Child_process():
   print ('Starting function')
   time.sleep(5)
   print ('Finished function')
P = multiprocessing.Process(target = Child_process)
P.start()
print("My Process has terminated, terminating main thread")
print("Terminating Child Process")
P.terminate()
print("Child Process successfully terminated")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM