H
H
hikionori2020-11-30 09:54:26
Python
hikionori, 2020-11-30 09:54:26

How can I make the script run all the time?

In general, the question is
I have a file with a voice assistant which I wrote in python. It uses the gTTS library for speech playback. After starting the file, it listens and if there are keywords, it performs some function and after that the file execution ends.
How to make it so that the execution does not end?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-11-30
@hikionori

Here is an example to study, 2 functions are executed independently of each other. For example, one with an infinite loop, the second with a finite one, the first will continue to be executed regardless of the fact that the second has ended.

from threading import Thread
from time import sleep


def print_some1(txt):
    while True:
        print(f'{print_some1.__name__} - {txt}')
        sleep(.5)


def print_some2(txt, n):
    while n < 10:
        n += 1
        print(f'{print_some2.__name__} - {txt}')
        sleep(.75)


if __name__ == '__main__':
    text = 'some text message'
    Thread(target=print_some1, args=(text,)).start()
    Thread(target=print_some2, args=(text, 1)).start()

S
Sergey Gornostaev, 2020-11-30
@sergey-gornostaev

I don't know how your code works, but I suspect that you need to listen in an infinite loop, running functions in separate threads.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question