欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python定时器使用示例分享

程序员文章站 2023-11-26 13:58:52
复制代码 代码如下:class sltimer(multiprocessing.process):    #from datetime imp...

复制代码 代码如下:

class sltimer(multiprocessing.process):
    #from datetime import datetime
    #import time

    def __init__(self, target=none, args=(), kwargs={},date=none,time=none):
        '''\
        @param date 1900-01-01
        @param time 00:00:00
        '''
        super(sltimer,self).__init__(target=target,args=args,kwargs=kwargs)
        _date = ''
        if date is none:
            _date = datetime.now().__str__()[:10]
        else :
            _date = date

        _time = ''
        if time is none:
            _time = datetime.now().__str__()[11:19]
        else:
            _time = time

        self.__runtime = '%s %s' % (_date,_time)

    def run(self):
        timelen = len('1900-00-00 00:00:00')

        while true:
            now = datetime.now().__str__()[:timelen]
            if now>=self.__runtime:
                break
            print 'sleeping... %s' % now
            time.sleep(1.0)

        super(sltimer,self).run()

测试

复制代码 代码如下:

def show1():
    from datetime import datetime
    print 'hello,current time:%s' % datetime.now().__str__()

def t23():
    from  stock.task import sltimer

    timer = sltimer(show1, time='16:31:50')
    timer.start()