python定时器使用示例分享

yipeiwu_com6年前Python基础

复制代码 代码如下:

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()

相关文章

python 利用pywifi模块实现连接网络破解wifi密码实时监控网络

python 利用pywifi模块实现连接网络破解wifi密码实时监控网络,具体内容如下: import pywifi from pywifi import * import tim...

老生常谈Python startswith()函数与endswith函数

函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一、函数说明 语法:string.startswith(str, beg=0,end=len(string)...

Python排序搜索基本算法之堆排序实例详解

Python排序搜索基本算法之堆排序实例详解

本文实例讲述了Python排序搜索基本算法之堆排序。分享给大家供大家参考,具体如下: 堆是一种完全二叉树,堆排序是一种树形选择排序,利用了大顶堆堆顶元素最大的特点,不断取出最大元素,并调...

python-opencv获取二值图像轮廓及中心点坐标的代码

python-opencv获取二值图像轮廓及中心点坐标代码: groundtruth = cv2.imread(groundtruth_path)[:, :, 0] h1, w1 =...

Python制作数据导入导出工具

python 2.6编写,自己瞎写的,备用 ''' Export and Import ElasticSearch Data. Simple Example At __mai...