Python模块学习之Timer定时任务,函数自调实现定时获取博客园博客浏览量
2018-02-08 23:36
696 查看
Timer定时任务
下面是Timer函数的官方doc介绍信息""" Call a function after a specified number of seconds: t = Timer(30.0, f, args=None, kwargs=None) t.start() t.cancel() # stop the timer's action if it's still waiting """
第一个参数时指定多长时间之后执行这个函数,第二个参数时调用的函数名,
后面两个是可选函数,作为传递函数需要使用的参数,可以传递普通的参数和字典
t.start() 启动这个定时任务,也可以使用t.cancel()在一定的条件来停止这个定时任务,
下面这行代码表示十秒钟后调用一次views_count这个函数
Timer(10, views_count).start()
自调任务实例
下面的这个实例利用threading.Timer()建立了一个自调任务,实现了每十秒请求一次博客园获取浏览量#! /usr/bin/python # coding:utf-8 """ @author:Administrator @file:Timer_test.py @time:2018/02/08 """ import requests import re from threading import Timer def views_count(): global count global source_view article_views = [] url = "http://www.cnblogs.com/Detector/default.html?page=%s" for i in range(1, 5): html = requests.get(url % i).text article_view = re.findall("_Detector 阅读\((.*?)\)", html) article_views += article_view count += 1 current_view = sum(map(lambda x: int(x), article_views)) if current_view - source_view > 50: print("You have made great progress") else: print("current_view: ", current_view) if count < 10000: # 运行一万次 Timer(10, views_count).start() count = 0 source_view = 2412 # 设定一个初始阅读数据 Timer(10, views_count).start()
相关文章推荐
- Python模块学习之Timer定时任务,函数自调实现定时获取博客园博客浏览量
- python学习笔记(三) 实现switch,内建函数,模块和包
- Python任务调度模块 – APScheduler,Flask-APScheduler实现定时任务
- 装饰器应用,Python模块学习之(__call__)实现准确计算函数运行时间
- Python模块学习 ---- httplib HTTP协议客户端实现
- Python模块学习 ---- fileinput - 成长的点滴,记录与分享 - 博客频道 - CSDN.NET
- Python模块学习 ---- httplib HTTP协议客户端实现
- Python模块学习 ---- httplib HTTP协议客户端实现
- Python: 通过Inspect模块获取函数上下文信息.
- java Timer实现定时任务
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/
- Python:使用threading模块实现多线程编程三[threading.Thread类的重要函数]
- .NET如何利用timer组件实现每天定时执行某项任务 .
- 学习定时任务的java实现
- Python:使用threading模块实现多线程编程三[threading.Thread类的重要函数]
- PHP 函数 ignore_user_abort定时执行任务的实现
- Python模块学习之os常用函数
- 定时任务模块,附带定时采集实现
- Python 学习:今天写了一个简单的定时任务