Python Forum
Simple mutli-threaded scheduler using sched - stuck
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple mutli-threaded scheduler using sched - stuck
#1
Hi there

I need help creating a scheduler that runs indefinitely.
While the scheduler is running users can schedule new events using enter or enterabs.

I’m struggling to do that using a thread or indefinite loop.

Please help
Reply
#2
I have a basic timer class here. With a simple modification, you can make events recurring, see: https://python-forum.io/Thread-Multi-thr...ight=timer
Reply
#3
Thanks Larz60+ but looking for a simple (i'm new to python) solution which involves the sched library

Here is my attempt so far. My problem is that as soon as scheduler events complete, nothing else runs
import sched
import threading
import time

scheduler = sched.scheduler(time.time, time.sleep)

# Set up a global to be modified by the threads
counter = 0


def increment_counter(name):
    global counter
    print('EVENT:', time.time(), name)
    counter += 1
    print('NOW:', counter)


print('START:', time.time())
e1 = scheduler.enter(5, 1, increment_counter, ('Liverpool match on TV',))
e2 = scheduler.enter(8, 1, increment_counter, ('Take the rubbish bin out',))

def worker():
    print("running worker")
    scheduler.run

# Start a thread to run the events
t = threading.Thread(target=worker, args=())
t.start()

# Back in the main thread, cancel the first scheduled event.
# scheduler.cancel(e1)

while True:
    print("1. Schedule an event")
    print("2. View all scheduled events")
    print("3. Cancel all")
    print("4. Exit")

    choice = int(input("enter option >> "))

    if choice == 1:
        print("Scheduling a new event...")
        e1 = scheduler.enter(3, 1, increment_counter, ('Some event in the future, remind me!',))
    if choice == 2:
        print("Queue status...")
        print(scheduler.queue)
    if choice == 3:
        print("Cancelling all events...")
        for ev in scheduler.queue:
            scheduler.cancel(ev)
    if choice == 4:
        print("Exiting..")
        break

print('FINAL:', counter)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running Python script through Task Scheduler? Winfried 8 484 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Improves performance on socket multi-threaded servers for object detection pennant 1 1,902 Aug-31-2021, 08:43 AM
Last Post: Larz60+
  Get return value from a threaded function Reverend_Jim 3 17,092 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  how to cancel scheduler module event nanok66 0 2,141 May-11-2020, 10:31 PM
Last Post: nanok66
  Getting an .exe to run from Task Scheduler fioranosnake 2 3,017 Dec-06-2019, 12:20 PM
Last Post: DeaD_EyE
  Batch file not running python script in task scheduler davork 3 4,525 May-09-2019, 12:53 PM
Last Post: Gribouillis
  sched.scheduler -> clean denisit 1 2,875 Nov-28-2018, 09:52 AM
Last Post: Gribouillis
  Windows 10 Task Scheduler Error mypython 1 2,491 Aug-11-2018, 11:01 PM
Last Post: ichabod801
  multi-threaded file parsing bb8 3 3,366 Apr-26-2018, 05:12 PM
Last Post: woooee
  Need help designing a multi-threaded solver 4Dummies 8 6,072 Jun-18-2017, 08:39 PM
Last Post: 4Dummies

Forum Jump:

User Panel Messages

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