简体   繁体   English

等到函数在Python中完成执行

[英]Wait until function finish executing in Python

I am new to Python 我是Python的新手

I have the codes below: 我有以下代码:

Inside my for there is another for loop then that 2nd for loop calls a function that does another for loop. 在我的内部有另一个for循环然后第二个for循环调用一个函数,它执行另一个for循环。 Now I want the 2nd for loop to wait until the 3rd loops finish its job. 现在我希望第二个for循环等到第三个循环完成它的工作。 How to do that? 怎么做?

Thanks 谢谢

def GetArea(img):
    os.chdir("C:\RJC Files\Thesis\Biometrics Thesis\Raw Data\Pics\PickedImages\\" + str(x) + "\\Process")
    for file2 in glob.glob("*.png"):
        # DO SOMETHING 
        # TAKE SOMETIME TO FINISH HERE.
-----------------------------------------------------------     
for x in range(1, 2):
    os.chdir("C:\RJC Files\Thesis\Biometrics Thesis\Raw Data\Pics\PickedImages\\" + str(x) + "\\Area")
    for file in glob.glob("*.png"):
        img = cv2.imread(file, 0)

        GetArea(img)

By default python runs synchronously (this is true of nearly all programming languages). 默认情况下,python同步运行(几乎所有编程语言都是如此)。 This means that any statement will completely finish before the next statement begins. 这意味着任何语句将在下一个语句开始之前完成。 You would have to go out of your way to change this behavior. 你必须不遗余力地改变这种行为。

As an aside: you have a problem where you refer to x inside your function but it is not passed in. This might work at the moment, because x is being treated as a global variable, but it would be more appropriate to let your function accept it, or to change your glob.glob invocation. 顺便说一下:你有一个问题,你在函数中引用x但是它没有被传入。这可能目前正在工作,因为x被视为一个全局变量,但让你的函数更合适接受它,或者更改你的glob.glob调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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