python定时器使用示例分享

时间:2023-04-06 15:03:57 


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,定时器
0
投稿

猜你喜欢

  • 让字体美起来

    2011-06-14 09:50:21
  • 基于Go Int转string几种方式性能测试

    2024-05-08 10:17:04
  • asp如何生成XML数据

    2007-08-20 09:50:00
  • Python使用re模块正则提取字符串中括号内的内容示例

    2022-06-15 14:20:04
  • Python学习笔记之函数的参数和返回值的使用

    2021-12-01 19:06:39
  • pytorch 实现模型不同层设置不同的学习率方式

    2023-11-20 00:29:25
  • 解决springboot yml配置 logging.level 报错问题

    2021-09-21 21:38:02
  • django实现日志按日期分割

    2023-07-20 04:25:21
  • Django中Middleware中的函数详解

    2023-08-30 06:58:30
  • 用户反馈对产品设计的帮助

    2009-02-09 13:15:00
  • CentOS7 64位下MySQL5.7安装与配置教程

    2024-01-17 16:32:12
  • ASP无组件上载,带进度条,多文件上载

    2008-10-29 10:03:00
  • Python os.access()用法实例

    2022-12-06 01:37:50
  • js 上传文件预览的简单实例

    2024-06-05 09:11:59
  • Python中如何获取类属性的列表

    2023-02-10 21:57:41
  • python读取nc数据并绘图的方法实例

    2023-09-16 10:08:19
  • 关于MySQL自增ID的一些小问题总结

    2024-01-22 18:52:56
  • python 实现"神经衰弱"翻牌游戏

    2023-02-08 05:34:05
  • Python编程super应用场景及示例解析

    2023-06-10 03:00:46
  • Sqlserver 2005使用XML一次更新多条记录的方法

    2024-01-28 19:50:04
  • asp之家 网络编程 m.aspxhome.com