A
A
Alexander Didenko2018-05-23 19:03:00
Python
Alexander Didenko, 2018-05-23 19:03:00

How to make a timer in python?

Python 2.7. Debian.
The program is in an eternal loop.
How to make print happen after 10 seconds?
But without stopping the cycle, other procedures also want to work there.
On arduino it was done like this:

timing = millis(); 
void loop() {
 if (millis() - timing > 10000){
  timing = millis(); 
  Serial.println ("10 seconds");
 }
}

It would be desirable to do without flows. And then there is no time to study what it is and how it works

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexander, 2018-05-23
@Bagunda

import time
timing = time.time()
while True:
    if time.time() - timing > 10.0:
        timing = time.time()
        print("10 seconds")

S
Sergey Gornostaev, 2018-05-23
@sergey-gornostaev

Yes, you can do the same:

from time import monotonic

t = monotonic()
while True:
    if monotonic() - t > 10:
        t = monotonic()
        print('tick')

K
khudobinvasiliy, 2019-10-31
@khudobinvasiliy

import time

print("О чём вам напомнить?")

text = str(input())

print("Через сколько минут?")

local_time = float(input())

local_time = local_time * 60

time.sleep(local_time)

print(text)

A
Alexander, 2018-05-23
@sanya84

from threading import Timer
from time import sleep

def hello():
    print("Hello user!")

def bye():
    sleep(5)
    print("Bye...bye...")

while 1:
    timer = Timer(3, hello)
    timer.start()
    bye()

Z
Zanak, 2018-05-27
@Zanak

You can also do something like this:

import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    exit()

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

for i in range(0, 1000000):
    print "i: %s\n" % (i)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question