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

 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) 
  • one
    can an infinite loop before try ? - pavel
  • each x must be successfully processed one time. in an infinite loop, the function will receive the same result all the time - MIKS
  • one
    extra break before except ? - pavel
  • for sure. thank. post in the answers will choose as a solution - MIKS
  • @KoVadim has the same solution. - pavel

3 answers 3

That's where it is. I also added a counter for unsuccessful attempts. If there were more than 1000 attempts, then it is not known if 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) 

    I would write like this

     def Scan_data(x): try: y = ... return y except: return None for i in data: while Scan_data(i) is None: time.timesleep(60) 

      Apply recursion:

      def Scan_data(x): try: y = <..Обращение к интернету..> except: time.timesleep(60) Scan_data(x) return y

      • 2
        When there is a problem. If the number of "hits" is more than 1000, then it will fall. - KoVadim