简体   繁体   中英

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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