In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to use the sched event Scheduler in the python package". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the sched event Scheduler in the python package.
1. Delayed running events
To execute an event after a delay or a specified time, you need to use the enter () method with the following parameters:
Interval time (the specific value is determined with delayfunc, in this case seconds)
Priority (which one executes first when two events arrive at the same time)
Called function
Function parameter
Import schedimport time# generation scheduler scheduler = sched.scheduler (time.time, time.sleep) def print_event (name): print ('EVENT:', time.time (), name) print (' START:', time.time ()) # set to execute the calling function scheduler.enter (2, 1, print_event, ('first',)) scheduler.enter (3, 1, print_event, (' second') after 2 seconds and 3 seconds respectively after execution ) # run the scheduler scheduler.run () # output result # START: 1532050215.377 EVENT: 1532050217.3747234 first# EVENT: 1532050218.375626 second II, overlapping events
Call the run () block to execute all events
Each event runs in the same thread, so if an event takes longer, delayed events will overlap.
In order not to lose the event, the deferred event will be executed after the previous event has been run
But some delayed events may be later than originally planned.
Import schedimport timescheduler = sched.scheduler (time.time, time.sleep) def long_event (name): print ('BEGIN EVENT:', time.time (), name) time.sleep (2) print (' FINISH EVENT:', time.time (), name) print ('START:', time.time ()) scheduler.enter (2, 1, long_event, (' first',)) # the event cannot be executed after the envisaged 3 seconds Scheduler.enter (3,1, long_event, ('second',)) scheduler.run () # output result # START: 1532051082.823752 output BEGIN EVENT: 1532051084.8392828 first# FINISH EVENT: 1532051086.8475456 first# BEGIN EVENT: 1532051086.8475456 second# FINISH EVENT: 1532051088.8557353 second III, event priority
If multiple events are executed at the same time, they are used to determine the sequence by setting their priority values.
Import schedimport timescheduler = sched.scheduler (time.time, time.sleep) def print_event (name): print ('EVENT:', time.time (), name) now = time.time () print (' START:', now) scheduler.enterabs (now+2, 2, print_event, ('first',)) scheduler.enterabs (now+2, 1, print_event, (' second') ) scheduler.run () # output result # START: 1532052567.605726 output EVENT: 1532052569.621258 second# EVENT: 1532052569.621258 first IV, cancel event
Use enter () and enterabs () to return a reference event to cancel it
Import schedimport threadingimport timescheduler = sched.scheduler (time.time, time.sleep) # set up a global thread counter counter = 0def increment_counter (name): global counter print ('EVENT:', time.time (), name) counter + = 1 print (' NOW:', counter) print ('START:', time.time ()) E1 = scheduler.enter (2, 1, increment_counter, (' E1,)) e2 = scheduler.enter (3, 1, increment_counter, ('E2') ) # start a thread execution event t = threading.Thread (target=scheduler.run) t.start () # in the main thread, cancel the first scheduled event scheduler.cancel (E1) # wait for the thread scheduler to finish running t.join () # output result # START: 1532053265.528012 thread EVENT: 1532053268.528813 eBay NOW: 1, other methods # determine whether the queue is empty scheduler.empty () # read-only attribute Return a list of upcoming events (sorted by arrival events). Each event is a namedtuplescheduler.queue composed of time, priority, action and argument. I believe you have a deeper understanding of "how to use the sched event scheduler in the python package". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.