-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
49 lines (41 loc) · 1.62 KB
/
Copy pathtimer.py
File metadata and controls
49 lines (41 loc) · 1.62 KB
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
40
41
42
43
44
45
46
47
48
49
import logging
import time
from pipeable_worker import PipeableWorker
log = logging.getLogger('PastebinCrawler')
class Timer(PipeableWorker):
"""
This worker is designed to be the first in the pipe and schedule it.
Input: Sleep interval
Output: Time slept is seconds
"""
POLL_INTERVAL = 0.2
def __init__(self, sleep_interval, worker_name=None):
"""
:param sleep_interval: How many seconds to sleep between
each work session.
:param worker_name: A name to be used in log messages.
Default to the class name.
"""
super().__init__(worker_name=worker_name)
self._sleep_interval = sleep_interval
def work(self, sleep_interval):
log.info(f'{self}: Started timer with an interval of '
f'{self._sleep_interval}')
# Run untill external shutdown
while not self._output_done_event.is_set():
self._add_to_out_queue(sleep_interval)
self.sleep(sleep_interval)
def sleep(self, seconds):
"""
Sleeps in intervals in order to check _output_done_event
"""
wake_time = time.time() + seconds
while time.time() < wake_time and not self._output_done_event.is_set():
time.sleep(self.POLL_INTERVAL)
log.info(f'{self}: Finished sleep')
def first_pipe_prepare(self):
"""
If this worker is the first in the pipe than still perform one request
"""
super().first_pipe_prepare()
self._add_to_input_queue(self._sleep_interval)