Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
schedule module conundrum
#11
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()
    print(msg)
    print(PROMPT, end='')
    sys.stdout.flush()

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

class Monitor(Thread):
    def __init__(self, delta_sensor, delta_report, threshold):
        Thread.__init__(self)
        self.ds = delta_sensor.total_seconds()
        self.delta_report = delta_report
        self.threshold = threshold
        
    def run(self):
        # self.resume = True
        self.next_report = dt.datetime.now() + self.delta_report
        while True:
            monitor_event.clear()
            monitor_event.wait(timeout=self.ds)
            if monitor_event.is_set():
                break
            temp = read_temperature()
            if temp > self.threshold:
                print_msg(
                    'Warning: temperature {:.1f} above threshold {:.1f}'.format(
                    temp, self.threshold))
            t = dt.datetime.now()
            if t >= self.next_report:
                print_msg(
                    '{:%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)
    monitor.start()
    try:
        while True:
            s = input(PROMPT)
            if s.strip().lower() in ('q', 'quit'):
                break
    finally:
        monitor_event.set()
        monitor.join()
Output:
λ python3 paillasse/cooler.py 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
Reply
#12
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.

Grateful..

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cancelling previous schedule cosmarchy 2 2,736 Sep-15-2021, 04:55 PM
Last Post: Larz60+
  Hosting a script on a VPS, and on a schedule? oldguy 0 2,918 Mar-13-2021, 02:46 AM
Last Post: oldguy
  something i noticed about the "schedule" module Stan2292 1 1,740 Aug-30-2019, 06:04 AM
Last Post: buran
  How to schedule Jupyter Notebooks with Papermill wendysling 0 2,478 Jun-11-2019, 05:53 PM
Last Post: wendysling
  I got a problem with python schedule darktitan 2 3,449 Sep-22-2018, 12:49 PM
Last Post: darktitan
  Schedule with other processing RValentim 7 3,938 Jul-10-2018, 12:57 PM
Last Post: RValentim
  How to convert Schedule to APScheduler module? penguin9 2 3,729 May-03-2018, 12:44 PM
Last Post: penguin9
  Creating a schedule program ndplokkaar 4 3,902 Nov-23-2017, 04:21 PM
Last Post: ndplokkaar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020