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
  The solution is probably simple, but I'm stuck, as an amateur is Wookie7602 3 883 Nov-13-2024, 11:16 PM
Last Post: jefsummers
Exclamation Multi-Threaded Camera Feed issue Khajababa69 0 1,472 May-05-2024, 09:44 PM
Last Post: Khajababa69
Question Running Python script through Task Scheduler? Winfried 8 6,411 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Improves performance on socket multi-threaded servers for object detection pennant 1 2,655 Aug-31-2021, 08:43 AM
Last Post: Larz60+
  Get return value from a threaded function Reverend_Jim 3 24,225 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  how to cancel scheduler module event nanok66 0 2,866 May-11-2020, 10:31 PM
Last Post: nanok66
  Getting an .exe to run from Task Scheduler fioranosnake 2 3,780 Dec-06-2019, 12:20 PM
Last Post: DeaD_EyE
  Batch file not running python script in task scheduler davork 3 5,958 May-09-2019, 12:53 PM
Last Post: Gribouillis
  sched.scheduler -> clean denisit 1 3,631 Nov-28-2018, 09:52 AM
Last Post: Gribouillis
  Windows 10 Task Scheduler Error mypython 1 3,019 Aug-11-2018, 11:01 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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