Sched Timing Event Scheduler for the Python standard library

Source: Internet
Author: User

#-*-Coding:utf-8-*-
# Author: Beginner
__author__ = ' Administrator '
#py标准库之sched
Import time
Import sched# Timer Event Scheduler
#使用time来掌握当前时间, there is a delay to specify a time period
#调用time是不带任何参数的, returns the current number of times, delay to provide an integer argument,
#有延迟运行事件
"""
4 methods
1: Indicates the number of delays;
2: Priority value;
3: the function to invoke;
4: Tuple of function arguments
"""
#例1:
Sche=sched.scheduler (Time.time,time.sleep)
def print_event (Name,start):
Now=time.time ()
End=int (Now-start)
Print ' event:%s end=%s name=%s '% (Time.ctime (now), End,name)
Start=time.time ()
print ' Statr: ', Time.ctime (start)
Sche.enter (2,1,print_event, (' first ', start)) #是2秒
Sche.enter (3,1,print_event, (' second ', start)) #3秒
Sche.run ()
#重叠事件
#run () calls are blocked, and even all events have been processed, and each event is run in the same purebred, so if an event takes a long time to run, the delay between events will overlap
#这个重叠可以通过推迟后面的事件来解决, such a move loses the event, but some events may be later than their dispatch time,
#例2
def longevent (name):
print ' 1event: ', Time.ctime (Time.time ()), name
Time.sleep (2)
print ' 2event ', Time.ctime (Time.time ()), name
print ' Start ', Time.ctime (Time.time ())
Sche.enter (2,1,longevent, (' first ',))
Sche.enter (3,1,longevent, (' second ',))
Sche.run ()
#当第一个事件一旦完成就会立即运行第二个事件 because the first event takes longer than the 2nd event, so it will exceed the start time of the second event

#事件优先级
#如果调度多个事件在同一个时间运行, use the priority of things to determine the order in which they will run
def yxevent (name):
print ' event ', Time.ctime (Time.time ()), name
Now=time.time ()
print ' Start: ', Time.ctime (now)
Sche.enter (2,1,yxevent, (' first ',))
Sche.enter (3,1,yxevent, (' second ',))
Sche.run ()

#取消事件
#enter () and Enterabs () will return a reference to an event that can be used later to cancel the event, which must be canceled in a different thread due to the block of Run ()
Import threading
Count=0
def count1 (name):
Global Count
print ' event ', Time.ctime (Time.time ()), name
Count+=1
print ' Now: ', count
print ' Start: ', Time.ctime (now)
A1=sche.enter (2,1,count1, (' A1 ',))
A2=sche.enter (3,1,count1, (' A2 ',))
T=threading. Thread (Target=sche.run)
T.start ()
Sche.cancel (A1)
T.join ()
print ' final: ', Count

#这边调用了2个事件, but the first one is then canceled and only the second event is run, so the count variable increments only once

#sched官方标准库: https://docs.python.org/2.7/library/sched.html?highlight=sched#module-sched

Sched Timing Event Scheduler for the Python standard library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.