python – How to loop a function so that its execution is guaranteed

Question:

The program accesses the Internet to obtain data from the list of source IDs. At night, the Internet often falls off, which leads to a program error. How to achieve trouble-free operation of the program so that the list item is guaranteed to be processed? What you need to write after the except: so that the program will re-access the try: block until it processes it with the correct result y .

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def Scan_data(x):
    try:
        y = <..Обращение к интернету..>
    except:
        time.timesleep(60)
        # <..Тут не знаю что написать..>
    return y

for i in data:
    Scan_data(i)

Answer:

That's where it is. I also added a failed attempts counter. If there were more than 1000 attempts, then it is not known whether it makes sense to continue.

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def Scan_data(x):
    y = -1
    happy = false
    tr = 0 # сколько раз пробовали
    while (!happy and tr < 1000):
        try:
            y = <..Обращение к интернету..>

            happy = true # как только поняли, что все ок
        except:
            time.timesleep(60)
            tr = tr + 1 # кол-во попыток
            print("попытка номер ", tr)
    return y

for i in data:
    Scan_data(i)
Scroll to Top