Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sched.scheduler -> clean
#1
Hi,

I have an issue with sched.scheduler
We need to cancel some upcoming events. (thats working)
but the sched.run()
will wait util the last event was scheduled.

import threading
import sched
import time
import datetime

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

def test():
    print(datetime.datetime.now(),"sleep 6s in main")
    time.sleep(6)
    print(datetime.datetime.now(),"sleep 6s in main Done")
    for x in _schedLed.queue:
       _schedLed.cancel(x)
       print(datetime.datetime.now(),"Cancel:", x)
    print(datetime.datetime.now(),"Cancel DONE")
                   
t = threading.Thread(target=test)
t.start()

_schedLed.enter(5,1,print,("5s"))
_schedLed.enter(20,1,print,("20s"))
_schedLed.run()
print(datetime.datetime.now(),"end")
sudo python3 test3.py
2018-11-26 10:07:13.561110 sleep 6s in main
5 s
2018-11-26 10:07:19.567805 sleep 6s in main Done
2018-11-26 10:07:19.568262 Cancel: Event(time=1543223253.5615287, priority=5, action=<built-in function print>, argument='10s', kwargs={})
2018-11-26 10:07:19.568575 Cancel DONE
2018-11-26 10:07:33.576680 end

Is there a way to fix that?

Regards

(Nov-26-2018, 09:12 AM)denisit Wrote: sudo python3 test3.py
2018-11-26 10:07:13.561110 sleep 6s in main
5 s
2018-11-26 10:07:19.567805 sleep 6s in main Done
2018-11-26 10:07:19.568262 Cancel: Event(time=1543223253.5615287, priority=5, action=<built-in function print>, argument='20s', kwargs={})
2018-11-26 10:07:19.568575 Cancel DONE
2018-11-26 10:07:33.576680 end
Reply
#2
I could make it work by using a custom delay function with a threading.Condition() object
import threading
import sched
import time
import datetime

K = threading.Condition()
def mydelayfunc(secs):
    with K:
        K.wait(timeout=secs)
 
_schedLed = sched.scheduler(time.time, mydelayfunc)
 
def test():
    print(datetime.datetime.now(),"sleep 6s in main")
    time.sleep(6)
    print(datetime.datetime.now(),"sleep 6s in main Done")
    for x in _schedLed.queue:
       _schedLed.cancel(x)
       print(datetime.datetime.now(),"Cancel:", x)
    with K:
        K.notify()
    print(datetime.datetime.now(),"Cancel DONE")
                    
t = threading.Thread(target=test)
t.start()
 
_schedLed.enter(5,1,print,("5s"))
_schedLed.enter(20,1,print,("20s"))
_schedLed.run()
print(datetime.datetime.now(),"end")
The Condition.wait() method is very handy because of its timeout argument and the notify() method. From my experience, it is also quite accurate. You can also do the same with a threading.Event instance (you don't need the with statement then).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running Python script through Task Scheduler? Winfried 8 335 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Can i clean this code ? BSDevo 8 849 Oct-28-2023, 05:50 PM
Last Post: BSDevo
  Clean Up Script rotw121 2 981 May-25-2022, 03:24 PM
Last Post: rotw121
  How to clean UART string Joni_Engr 4 2,411 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  how to cancel scheduler module event nanok66 0 2,097 May-11-2020, 10:31 PM
Last Post: nanok66
  How to clean session mqtt SayHiii 0 1,968 Dec-09-2019, 07:56 AM
Last Post: SayHiii
  Getting an .exe to run from Task Scheduler fioranosnake 2 2,974 Dec-06-2019, 12:20 PM
Last Post: DeaD_EyE
  Simple mutli-threaded scheduler using sched - stuck Mzarour 2 6,049 Nov-12-2019, 07:44 PM
Last Post: Mzarour
  how to clean up unstarted processes? Skaperen 2 2,201 Aug-27-2019, 05:37 AM
Last Post: Skaperen
  Batch file not running python script in task scheduler davork 3 4,475 May-09-2019, 12:53 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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