Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until function finish executing in Python

Tags:

python

I am new to 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. Now I want the 2nd for loop to wait until the 3rd loops finish its job. 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)
like image 641
wal Avatar asked Nov 20 '25 08:11

wal


1 Answers

By default python runs synchronously (this is true of nearly all programming languages). 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.

like image 120
Cireo Avatar answered Nov 22 '25 23:11

Cireo