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()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
让字体美起来
基于Go Int转string几种方式性能测试
asp如何生成XML数据
Python使用re模块正则提取字符串中括号内的内容示例
Python学习笔记之函数的参数和返回值的使用
![](https://img.aspxhome.com/file/2023/4/114724_0s.png)
pytorch 实现模型不同层设置不同的学习率方式
解决springboot yml配置 logging.level 报错问题
django实现日志按日期分割
![](https://img.aspxhome.com/file/2023/5/72885_0s.jpg)
Django中Middleware中的函数详解
用户反馈对产品设计的帮助
![](https://img.aspxhome.com/file/UploadPic/20092/9/blog02081-75s.jpg)
CentOS7 64位下MySQL5.7安装与配置教程
![](https://img.aspxhome.com/file/2023/1/107911_0s.jpg)
ASP无组件上载,带进度条,多文件上载
Python os.access()用法实例
js 上传文件预览的简单实例
Python中如何获取类属性的列表
python读取nc数据并绘图的方法实例
![](https://img.aspxhome.com/file/2023/8/91868_0s.png)
关于MySQL自增ID的一些小问题总结
python 实现"神经衰弱"翻牌游戏
![](https://img.aspxhome.com/file/2023/6/92236_0s.gif)
Python编程super应用场景及示例解析
![](https://img.aspxhome.com/file/2023/7/117907_0s.png)