'Code should kill the terminal before generating a loop because it found the value but it is only killing it after looping

For this project I'm using Visual Studio Code on Windows 11

In this file example StackOverflowTest.py, if there are 5 values in the list, I want the terminal to be killed:

import os
import signal

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

def test_kill():
    list_test = [1,2,3,4,5]
    if (len(list_test) == 5):
        kill_terminal()

def main():
    print('Start Process')
    test_kill()

I created a loop to activate the main() function from the previous file, but in theory it was to kill the terminal on the first call that is made, but that doesn't happen, it only kills the terminal in the second looping:

from random import randint
import time
import schedule
import StackOverflowTest
import sys

StackOverflowTest.main()
def trigger():
    StackOverflowTest.main()
schedule.every(1).seconds.do(trigger)

while 1:
    schedule.run_pending()
    for remaining in range(randint(5,10), 0, -1):
        sys.stdout.write('\r')
        sys.stdout.write('Next activation in {:2d} seconds'.format(remaining))
        sys.stdout.flush()
        time.sleep(1)
    sys.stdout.write('\r                               \n')

The correct way it should happen would be like this:
As it finds the value 5, the correct thing was to kill the terminal after the first Start Process.

Start Process
~~~~KILL TERMINAL~~~~

However as it is not working correctly, it is happening like this:

Start Process
Next activation in 3 seconds
Next activation in 2 seconds
Next activation in 1 seconds
Start Process
~~~~KILL TERMINAL~~~~

As can be seen he kill the terminal only in the second time run main().

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