英文:
How to exit a function from another function?
问题
要在另一个函数中的语句为真时退出函数。我正在使用多进程,所以它们同时运行。
我想做类似这样的事情:
def A():
while True:
if condition is True:
#做一些事情
else:
pass
def B():
while True:
if condition is True:
#停止A()函数
break
else:
pass
def main():
p1 = multiprocessing.Process(target=A)
p2 = multiprocessing.Process(target=B)
p1.start()
p2.start()
p1.join()
p2.join()
if __name__ == '__main__':
main()
英文:
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()
答案1
得分: 1
有多个同步原语可供使用,例如条件变量:
你可以使用 mp.Condition() 进行调用:
while not predicate():
cv.wait()
在 A()
中,以及在 B()
中使用 notify()
。
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论