'How to keep code active in a loop that needs a break in a loop of other code

I have a code called Trigger.py that works in looping by making a call to another code called Pausar_Looping.py:

from time import sleep
import schedule
import datetime
import Pausar_Looping

Pausar_Looping.main()
print("-----------------",datetime.datetime.now().strftime('%d/%m/%Y %H:%M'),"-----------------")

def trigger():
    Pausar_Looping.main()
    print("-----------------",datetime.datetime.now().strftime('%d/%m/%Y %H:%M'),"-----------------")

schedule.every(30).seconds.do(trigger)

while 1:
    schedule.run_pending()
    sleep(1)

The Pausar_Looping.py code has a chain of events also in looping where it needs a pause to avoid repeating the second part of the code several times, I called this second part to print('Continue'):

from random import randrange
import os
import signal
import sys
import time

def kill_terminal():
    os.kill(os.getppid(), signal.SIGTERM)

def one():
    number = randrange(3)
    1/number
    return number

def main(sleep_time=0):
    try:
        one()
        if (one() == 2):
            kill_terminal()
    except:
        while True:
            print('Error ONE')
            sleep_time += 1
            print('Next attempt in: '+ str(sleep_time) + ' second(s)')
            time.sleep(sleep_time)
            main(sleep_time=sleep_time)
            sys.exit(1)

    print('Continue')

This division of the number 1 by the number zero is just a demonstration of the code, because the real code makes a call to a JSON request that can give an error if it exceeds the 5 second call timeout.

Therefore, when there is an error in one(), it must return to the beginning of main() and try again as many times as necessary, passing to print('Continue') only when everything is correct. So on each call, regardless of how many errors there are, print('Continue') can only appear once.

This works perfectly, but as to make this break so I don't go to the second part of the code, I used sys.exit(1), here comes the problem:

The sys.exit(1) ends up disabling the looping of the Trigger.py code, causing the terminal ends the work after going through print('Continue'). I wanted it to keep making calls to Pausar_Looping.py every 30 seconds whenever everything went right.

How should I go about solving this problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source