Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 schedule module conundrum
I don't know why you can't do it with threading. Here is my code. It reads the sensor every second and warns if the threshold is reached. It also prints a temperature report every 5 seconds (python 3.5 code). It also does all this while waiting for user input to exit gracefully and potentially do other actions.
import datetime as dt
import random
import sys
from threading import Thread, Event
monitor_event = Event()
PROMPT = 'Type quit to quit: '

def print_msg(msg):
    print(PROMPT, end='')

def read_temperature():
    temp = random.gauss(30, 7)
    # print('reading', temp)
    return temp

class Monitor(Thread):
    def __init__(self, delta_sensor, delta_report, threshold):
        self.ds = delta_sensor.total_seconds()
        self.delta_report = delta_report
        self.threshold = threshold
    def run(self):
        # self.resume = True
        self.next_report = + self.delta_report
        while True:
            if monitor_event.is_set():
            temp = read_temperature()
            if temp > self.threshold:
                    'Warning: temperature {:.1f} above threshold {:.1f}'.format(
                    temp, self.threshold))
            t =
            if t >= self.next_report:
                    '{:%Y-%m-%d %H:%M:%S}: current temperature is {:.1f}'.format(
                        t, temp))
                self.next_report = t + self.delta_report

if __name__ == "__main__":
    monitor = Monitor(dt.timedelta(seconds=1), dt.timedelta(seconds=5), 40)
        while True:
            s = input(PROMPT)
            if s.strip().lower() in ('q', 'quit'):
λ python3 paillasse/ Type quit to quit: 2019-05-29 08:12:04: current temperature is 27.4 Type quit to quit: 2019-05-29 08:12:09: current temperature is 36.9 Type quit to quit: 2019-05-29 08:12:14: current temperature is 25.2 Type quit to quit: 2019-05-29 08:12:19: current temperature is 25.3 Type quit to quit: 2019-05-29 08:12:24: current temperature is 28.4 Type quit to quit: 2019-05-29 08:12:29: current temperature is 34.4 Type quit to quit: 2019-05-29 08:12:34: current temperature is 26.3 Type quit to quit: Warning: temperature 46.6 above threshold 40.0 Type quit to quit: Warning: temperature 41.8 above threshold 40.0 Type quit to quit: 2019-05-29 08:12:39: current temperature is 29.3 Type quit to quit: 2019-05-29 08:12:44: current temperature is 31.3 Type quit to quit: Warning: temperature 42.2 above threshold 40.0 Type quit to quit: quit
wgovideo likes this post
Your expertise is amazing. You gave me a different approach to the problem. Thank you very much for the code example. And, thanks again for your help and patience.


If I say the word python around here, people think I'm going to the zoo.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  something i noticed about the "schedule" module Stan2292 1 84 Aug-30-2019, 06:04 AM
Last Post: buran
  How to schedule Jupyter Notebooks with Papermill wendysling 0 385 Jun-11-2019, 05:53 PM
Last Post: wendysling
  I got a problem with python schedule darktitan 2 508 Sep-22-2018, 12:49 PM
Last Post: darktitan
  Schedule with other processing RValentim 7 1,030 Jul-10-2018, 12:57 PM
Last Post: RValentim
  How to convert Schedule to APScheduler module? penguin9 2 961 May-03-2018, 12:44 PM
Last Post: penguin9
  Creating a schedule program ndplokkaar 4 1,063 Nov-23-2017, 04:21 PM
Last Post: ndplokkaar

Forum Jump:

Users browsing this thread: 1 Guest(s)