[英]How to wait for function to finish inside while loop?
A simplified structure of my code is below.我的代码的简化结构如下。 I'm calling a function from inside a while loop, but the function called form inside that function needs to finish before the next iteration of the while loop.
我在 while 循环中调用了 function,但是 function 在 function 内部调用了表单,需要在 while 循环的下一次迭代之前完成。 I've tried threading as below but it will give me
RuntimeError: main thread is not in main loop
.我已经尝试过如下线程,但它会给我
RuntimeError: main thread is not in main loop
。
What would be a good way to go about this?什么是 go 关于这个的好方法?
def mainFunction():
#some code
def secondFunction():
#some code
mainFunction()
def thirdfunction():
#some code
def funcInFunc():
#some code
secondFunction()
def fourthFunction():
#some code
while [condition]:
#some code
#here calling funcInFunc() inside thirdFunction()
funcInFunc()
#need to wait for mainFunction() called from secondFunction() called from funcInFunc() to
finish before next iteration of this while loop
#WAIT FOR mainFunction() TO FINISH
#Tried but didn't work:
t = threading.Thread(target=funcInFunc, args=())
t.start()
while t_isAlive():
pass
Hard to do it without a real example.没有一个真实的例子很难做到。 So I try to explain my thoughts with your pseudo-code.
所以我试着用你的伪代码来解释我的想法。 I would use a global _flag variable, to authorize the following of the thread.
我将使用全局 _flag 变量来授权线程的以下内容。
def mainFunction():
#some code
# end of mainFunction
_flag = 0
def secondFunction():
#some code
mainFunction()
def thirdfunction():
#some code
def funcInFunc():
#some code
_flag = 1
secondFunction()
def fourthFunction():
#some code
while [condition]:
#some code
#here calling funcInFunc() inside thirdFunction()
funcInFunc()
#need to wait for mainFunction() called from secondFunction() called from funcInFunc() to finish before next iteration of this while loop
if _flag == 1
#WAIT FOR mainFunction() TO FINISH
if _flag == 0
Don't forget to declare it global where useful.不要忘记在有用的地方声明它是全局的。 When _flag == 1, it can go on.
当_flag == 1时,它可以go on。 When _flag == 0, it can iterate
当_flag == 0时,可以迭代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.