1

Hi I'm trying to write a python script. I'm totally new to this. What I'm wondering is if there is any way to make the script end by itself after running for five seconds? Happy if anybody knows? Thanks!

#!/usr/bin/env python
# Display a runtext with double-buffering.
from samplebase import SampleBase
from rgbmatrix import graphics 
import time
import sys


class RunText(SampleBase):
    def __init__(self, *args, **kwargs):
        super(RunText, self).__init__(*args, **kwargs)
        self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="hello")
        time.sleep(4)
        
    def run(self):
        offscreen_canvas = self.matrix.CreateFrameCanvas()
        font = graphics.Font()
        font.LoadFont("../../../fonts/5.bdf")
        textColor = graphics.Color(255, 255, 0)
        pos = offscreen_canvas.width
        my_text = self.args.text
    

        while True:
            offscreen_canvas.Clear()
            len = graphics.DrawText(offscreen_canvas, font, 0, 13, textColor, my_text,)
            offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
            
            
# Main function
if __name__ == "__main__":
    run_text = RunText()
    if (not run_text.process()):
        run_text.print_help()
Kollaps
  • 11
  • 1

1 Answers1

0

so it is pretty much like this:

import time, sys

time.sleep(5)
sys.exit()

or this:

import time

time.sleep(5)
exit()

or this option if running other processes in parallel:

import time, _thread, os


def stop(seconds):
    time.sleep(seconds)
    print('finished')
    os._exit(0)


_thread.start_new_thread(stop, (5,))

while True:
    print('running')
    time.sleep(1)

though I am not sure if such os._exit() is good or if it supported on all os

Matiiss
  • 5,970
  • 2
  • 12
  • 29