python-3.xmultiprocessingpython-multiprocessingkill-process

Multiprocessing: Two processes, kill both together


I have two processes in a python programme, using the multiprocessing framework. Each of them runs a function and each of them has a termination condition. If that happens, I want both to be killed.

from multiprocessing import Process, Value, Array, Lock, Queue, active_children
import time
import random
from queue import Full

def producer_func(q:Queue, quantity):
    if condition:
       pass

def consumer_func(q:Queue, quantity, producer):
    if condition:
       producer.kill()

def main():
    q = Queue(10)
    quantity = 20

    producer = Process(name="producer", target=producer_func, args=(q, quantity))
    consumer = Process(name="consumer", target=consumer_func, args=(q, quantity, producer))

    producer.start()
    consumer.start()

    producer.join()
    consumer.join()

if __name__ == "__main__":
    main()

I can somehow kill the producer from the consumer, but I cannot do the same trick the other way around, since the consumer is not defined yet. Is there a nice trick to use?


Solution

  • Use a multiprocess.Event.

    All workers should regularly check if this Event is set, and should return when it is.

    The Event can be set from one of the workers or the parent.

    def producer_func(q, quantity, condition):
        if condition.is_set():
           return
    
    def consumer_func(q:Queue, quantity, producer, condition):
        if condition.is_set():
           return
    
    
    def main():
        q = Queue(10)
        exit_event = multiprocessing.Event()
        quantity = 20
    
        producer = Process(name="producer", target=producer_func, args=(q, quantity, exit_event))
        consumer = Process(name="consumer", target=consumer_func, args=(q, quantity, exit_event))
    
        producer.start()
        consumer.start()
    
        producer.join()
        consumer.join()
    
    if __name__ == "__main__":
        main()