forked from rix1337/FeedCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
39 lines (31 loc) · 1.18 KB
/
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
# RSScrawler - Version 2.0.9
# Projekt von https://github.com/rix1337
# Enthält Code von:
# https://github.com/dmitryint (im Auftrag von https://github.com/rix1337)
from threading import Timer
import time
class RepeatableTimer(object):
def __init__(self, interval, function, args=(), kwargs={}):
self._interval = interval
self._function = function
self._args = args
self._kwargs = kwargs
self._START_TIME = None
self._TIMER_STARTED = False
def start(self):
if not self._TIMER_STARTED:
self._timer = Timer(self._interval, self._function, args=self._args, kwargs=self._kwargs)
self._timer.start()
self._START_TIME = time.time()
self._TIMER_STARTED = True
def cancel(self):
if self._TIMER_STARTED:
self._timer.cancel()
self._TIMER_STARTED = False
def running(self):
return self._TIMER_STARTED
def elapsed(self):
return time.time() - self._START_TIME if self._TIMER_STARTED else None
def remain(self):
return self._interval - (time.time() - self._START_TIME) if self._TIMER_STARTED else None