How to make a timer in Python 3?

Asked

Viewed 1,354 times

0

I have to make a timer that counts x seconds, but does not stay in a loop waiting for time to pass, instead it should count the time and generate an event when closing the preset time count. I need to know if there is any module in Python 3 that can perform this function and that has a start and a reset in the count?

1 answer

2


As I quoted in the answer below

Timeout in Python input function

You can use the module signal, on Unix systems, to call a function after a certain time, without blocking the execution of the main program.

import signal

def evento(*args):
    print("Evento disparado")

signal.signal(signal.SIGALRM, evento)
signal.alarm(5)

resposta = input('Por favor, aguarde 5 segundos...')

See working on Repl.it | Github GIST

For Windows, you can adapt the solution I presented in the other answer in the same way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.