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?