Home:ALL Converter>How to continuously run a python script until an output is printed?

How to continuously run a python script until an output is printed?

Ask Time:2019-08-07T22:20:33         Author:I_Literally_Cannot

Json Formatter

I am trying to get a python program to continuously run until a certain aws log is registered and printed. It is supposed to:

  1. Run indefinitely even if no events happen
  2. List whatever events occurs (in my case a log stating that the task is finished)
  3. Stop running

the python command looks like this: python3 watch_logs.py <log-source> --start=15:00:00

The logs are working fine, and the python script can print them out between certain time frames as long as they already exist. The program works by taking a continuously running task which prints events to the log file, and the python script should filter out events I am looking for and print them.

But, when I run the script, it wont print the event even if I can see the log entry appear in the file. If i kill the process and run it again using the same timestamp, it will find the log entry and end the script like it should.

the code is fairly short:

logs = get_log_events(
                log_group=log_group,
                start_time=start_time,
                end_time=end_time
            )
while True:
    for event in logs:
        print(event['message'].rstrip())
        sys.exit("Task complete")

Any insight why this is happening would help a lot. I am fairly new to python

Author:I_Literally_Cannot,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57396563/how-to-continuously-run-a-python-script-until-an-output-is-printed
Orion :

The value in the variable logs is old when the file is updated. You need to update this variable. For example if you were to use logs = myfile.read() at start of your script, the value in the logs variable would be a snapshot of that file at that time.",
2019-08-07T14:44:58
PMM :

Try storing event['message'].rstrip() in a variable and checking with an if statement if it corresponds to the log you want to find",
2019-08-07T14:27:58
Hans-Martin Mosner :

If you don't want to read the file each time through the loop, you should have a look at pygtail (https://pypi.org/project/pygtail/).",
2019-08-07T14:47:51
yy