Python Forum
Stopping scheduler with condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stopping scheduler with condition
#1
Hello,
I need to execute some code repeatedly with a fixed interval. I scraped this piece of code online:

class PeriodicScheduler(object):
    def __init__(self):
        self.scheduler = sched.scheduler(time.time, time.sleep)

    def setup(self, interval, action, actionargs=()):
        action(*actionargs)
        self.event = self.scheduler.enter(interval, 1, self.setup, (interval, action, actionargs))

    def run(self):
        self.scheduler.run()

    def stop(self):
        self._running = False
        if self.scheduler and self.event:
            self.scheduler.cancel(self.event)


# This is the event to execute every time
def periodic_event():
    print("hello")

# Start the scheduler
INTERVAL = 1
periodic_scheduler = PeriodicScheduler()
periodic_scheduler.setup(INTERVAL, periodic_event)  # it executes the event just once
periodic_scheduler.run()  # it starts the scheduler
The code works fine, it prints "hello" every second. I must say I never used schedulers before and so far didn't study the shown code deeply, so I don't fully understand how it works. But I would like to modify it so that the scheduled event only executes a given number of times.

My attempt was to add a counter variable inside periodic_event(), make checks (e.g. if cnt < 10), and if condition is satisfied call periodic_scheduler.stop(). But I couldn't make it work with all the scope errors I got. So I believe the approach wasn't a good one and there is probably a more obvious, standard solution, that I am not aware of.

What do you suggest me to do in this case? Did I even take the right direction in achieving the goal (executing a function specific number of times with fixed interval)?
Thank you,
JC
Reply


Messages In This Thread
Stopping scheduler with condition - by j.crater - Dec-22-2016, 08:08 AM
RE: Stopping scheduler with condition - by wavic - Dec-22-2016, 09:31 AM
RE: Stopping scheduler with condition - by j.crater - Dec-22-2016, 09:40 AM
RE: Stopping scheduler with condition - by wavic - Dec-22-2016, 09:46 AM
RE: Stopping scheduler with condition - by Larz60+ - Dec-22-2016, 12:30 PM
RE: Stopping scheduler with condition - by j.crater - Jan-03-2017, 09:19 AM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 10:53 AM
RE: Stopping scheduler with condition - by j.crater - Jan-03-2017, 01:06 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 01:09 PM
RE: Stopping scheduler with condition - by j.crater - Jan-03-2017, 01:12 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 01:15 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 05:32 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 10:50 PM
RE: Stopping scheduler with condition - by j.crater - Jan-04-2017, 06:36 AM
RE: Stopping scheduler with condition - by j.crater - Jan-09-2017, 03:02 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-09-2017, 07:28 PM
RE: Stopping scheduler with condition - by j.crater - Jan-10-2017, 06:31 AM
RE: Stopping scheduler with condition - by Larz60+ - Jan-10-2017, 11:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running Python script through Task Scheduler? Winfried 8 533 Mar-10-2024, 07:24 PM
Last Post: Winfried
  stopping number conversion before end Skaperen 6 3,019 Jul-12-2020, 09:22 AM
Last Post: DeaD_EyE
  Stopping a loop in another file catosp 4 2,702 Jun-15-2020, 02:45 PM
Last Post: catosp
  else condition not called when if condition is false Sandz1286 10 5,915 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,039 Jun-01-2020, 06:00 PM
Last Post: penahuse
  how to cancel scheduler module event nanok66 0 2,154 May-11-2020, 10:31 PM
Last Post: nanok66
  Getting an .exe to run from Task Scheduler fioranosnake 2 3,030 Dec-06-2019, 12:20 PM
Last Post: DeaD_EyE
  Simple mutli-threaded scheduler using sched - stuck Mzarour 2 6,152 Nov-12-2019, 07:44 PM
Last Post: Mzarour
  Batch file not running python script in task scheduler davork 3 4,537 May-09-2019, 12:53 PM
Last Post: Gribouillis
  Help with Stopping a function after a timer SheeppOSU 0 1,947 Jan-28-2019, 10:13 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

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