0

I want to exit a function in another function if statement is True. I am using multiprocessing so they are running concurrently.

I want to make something like this:

def A():
    while True:
        if condition is True:
            #do something
        else:
            True

def B():
    while True:
        if condition is True:
            #stop A() function
        else:
            True

def main():
    p1 = multiprocessing.Process(target=A())
    p2 = multiprocessing.Process(target=B())
    p1.start()
    p2.start()
    p1.join()
    p2.join()

if __name__ == '__main__':
    main()
5
  • 3
    What makes you think A() will be running simultaneously with B()? Commented Mar 7, 2023 at 14:15
  • Sorry, i don't quite understand. I use multiprocessing so they should run together... Commented Mar 7, 2023 at 14:18
  • Show us the threading code then Commented Mar 7, 2023 at 14:21
  • There, i edited the post. Commented Mar 7, 2023 at 14:25
  • You probably meant target=A? Commented Mar 7, 2023 at 14:26

1 Answer 1

1

There are multiple synchronization primitives you could use, such as a condition variable:

You could use a mp.Condition() calling

while not predicate():
    cv.wait()

in A() and notify() in B()

Sign up to request clarification or add additional context in comments.

3 Comments

I need the A() function to run while waiting. Is that possible?
You could have a 3rd process running which destroys the process where A is running in as soon as the condition is fulfilled
Can you provide an example?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.