python定时执行指定函数的方法
本文实例讲述了python定时执行指定函数的方法。分享给大家供大家参考。具体实现方法如下:
# time a function using time.time() and the a @ function decorator # tested with Python24 vegaseat 21aug2005 import time def print_timing(func): def wrapper(*arg): t1 = time.time() res = func(*arg) t2 = time.time() print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0) return res return wrapper # declare the @ decorator just before the function, invokes print_timing() @print_timing def getPrimeList(n): """ returns a list of prime numbers from 2 to < n using a sieve algorithm""" if n < 2: return [] if n == 2: return [2] # do only odd numbers starting at 3 s = range(3, n+1, 2) # n**0.5 may be slightly faster than math.sqrt(n) mroot = n ** 0.5 half = len(s) i = 0 m = 3 while m <= mroot: if s[i]: j = (m*m-3)//2 s[j] = 0 while j < half: s[j] = 0 j += m i = i+1 m = 2*i+3 return [2]+[x for x in s if x] if __name__ == "__main__": print "prime numbers from 2 to <10,000,000 using a sieve algorithm" primeList = getPrimeList(10000000) time.sleep(2.5) """ my output --> prime numbers from 2 to <10,000,000 using a sieve algorithm getPrimeList took 4750.000 ms """
希望本文所述对大家的Python程序设计有所帮助。
相关推荐
-
python定时检查某个进程是否已经关闭的方法
本文实例讲述了python定时检查某个进程是否已经关闭的方法.分享给大家供大家参考.具体如下: import threading import time import os import subprocess def get_process_count(imagename): p = os.popen('tasklist /FI "IMAGENAME eq %s"' % imagename) return p.read().count(imagename) def timer_star
-
python通过线程实现定时器timer的方法
本文实例讲述了python通过线程实现定时器timer的方法.分享给大家供大家参考.具体分析如下: 这个python类实现了一个定时器效果,调用非常简单,可以让系统定时执行指定的函数 下面介绍以threading模块来实现定时器的方法. 使用前先做一个简单试验: import threading def sayhello(): print "hello world" global t #Notice: use global variable! t = threading.Timer(5
-
用Python编写简单的定时器的方法
下面介绍以threading模块来实现定时器的方法. 首先介绍一个最简单实现: import threading def say_sth(str): print str t = threading.Timer(2.0, say_sth,[str]) t.start() if __name__ == '__main__': timer = threading.Timer(2.0,say_sth,['i am here too.']) timer.start() 不清楚在某些特殊应用场景下有什么缺陷
-
python单线程实现多个定时器示例
单线程实现多个定时器 NewTimer.py 复制代码 代码如下: #!/usr/bin/env python from heapq import *from threading import Timerimport threadingimport uuidimport timeimport datetimeimport sysimport math global TimerStampglobal TimerTimes class CancelFail(Exception): pass c
-
python定时检查启动某个exe程序适合检测exe是否挂了
详见代码如下: 复制代码 代码如下: import threading import time import os import subprocess def get_process_count(imagename): p = os.popen('tasklist /FI "IMAGENAME eq %s"' % imagename) return p.read().count(imagename) def timer_start(): t = threading.Timer(120,
-
Python3中常用的处理时间和实现定时任务的方法的介绍
无论哪种编程语言,时间肯定都是非常重要的部分,今天来看一下python如何来处理时间和python定时任务,注意咯:本篇所讲是python3版本的实现,在python2版本中的实现略有不同,有时间会再写一篇以便大家区分. 1.计算明天和昨天的日期 #! /usr/bin/env python #coding=utf-8 # 获取今天.昨天和明天的日期 # 引入datetime模块 import datetime #计算今天的时间 today = datetime.date.today() #计算
-
Python写的一个定时重跑获取数据库数据
做大数据的童鞋经常会写定时任务跑数据,由于任务之间的依赖(一般都是下游依赖上游的数据产出),所以经常会导致数据获取失败,因为很多人发现数据失败后 都会去查看日志,然后手动去执行自己的任务.下面我实现了一个自动重复执行去数据库取数,如果失败后自动重新去获取,直到把数据获取到. 建数据表: CREATE TABLE `testtable` ( 2 `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 3 `name` varchar(20) NOT NULL,
-
python实现定时播放mp3
程序很简单,主要是 mp3play 模块的应用 import mp3play, time filename = "Should It Matter.mp3" clip = mp3play.load(filename) while 1: if time.localtime().tm_min % 30 == 0: clip.play() print "\nStart to play" time.sleep(clip.seconds()) clip.stop() prin
-
python定时器(Timer)用法简单实例
本文实例讲述了python定时器(Timer)用法.分享给大家供大家参考.具体如下: # encoding: UTF-8 import threading #Timer(定时器)是Thread的派生类, #用于在指定时间后调用一个方法. def func(): print 'hello timer!' timer = threading.Timer(5, func) timer.start() 该程序可实现延迟5秒后调用func方法的功能. 希望本文所述对大家的Python程序设计有所帮助.
-
python定时执行指定函数的方法
本文实例讲述了python定时执行指定函数的方法.分享给大家供大家参考.具体实现方法如下: # time a function using time.time() and the a @ function decorator # tested with Python24 vegaseat 21aug2005 import time def print_timing(func): def wrapper(*arg): t1 = time.time() res = func(*arg) t2 = t
-
PHP递归调用数组值并用其执行指定函数的方法
本文实例讲述了PHP递归调用数组值并用其执行指定函数的方法.分享给大家供大家参考.具体分析如下: 以下为wordpress原代码,为了偷懒,简单修改一下以适用其它函数 /** * Navigates through an array and removes slashes from the values. * * If an array is passed, the array_map() function causes a callback to pass the * value back t
-
JavaScript实现当网页加载完成后执行指定函数的方法
本文实例讲述了JavaScript实现当网页加载完成后执行指定函数的方法.分享给大家供大家参考.具体分析如下: 下面的JS代码演示了如何在网页加载完成时调用指定的函数,并且可以通过第二段代码动态添加多个函数同时执行. 我们只需要给window.onload指定一个函数既可以在页面加载完成时自动执行MyCoolInitFunc函数 <script type="text/javascript" > window.onload = MyCoolInitFunc </scri
-
python每隔N秒运行指定函数的方法
本文实例讲述了python每隔N秒运行指定函数的方法.分享给大家供大家参考.具体如下: 这是一个类似定时器的效果,每隔指定的秒数运行指定的函数,采用线程实现,代码简单实用. 复制代码 代码如下: import os import time def print_ts(message): print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message) def run(
-
Python中用字符串调用函数或方法示例代码
前言 本文主要给大家介绍了关于Python用字符串调用函数或方法的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 先看一个例子: >>> def foo(): print "foo" >>> def bar(): print "bar" >>> func_list = ["foo","bar"] >>> for func in func_li
-
Python定时执行之Timer用法示例
本文实例讲述了Python定时执行之Timer用法.分享给大家供大家参考.具体分析如下: java中Timer的作用亦是如此.python中的线程提供了java线程功能的子集. #!/usr/bin/env python from threading import Timer import time timer_interval=1 def delayrun(): print 'running' t=Timer(timer_interval,delayrun) t.start() while T
-
Python自动化测试selenium指定截图文件名方法
目录 前言: 一.python中时间日期格式化符号 二.使用步骤 1.导入time模块,webdriver类 2.实际代码操作 总结: 前言: Selenium 支持 Web 浏览器的自动化,它提供一套测试函数,用于支持 Web 自动化测试.函数非常灵活,能够完成界面元素定位.窗口跳转.结果比较等功能.支持多种浏览器.多种编程语言(Java.C#.Python.Ruby.PHP 等).支持多种操作系统(Windows.Linux.IOS.Android 等).开源免费. 它主要由三个工具组成:W
-
JS动态插入并立即执行回调函数的方法
本文实例讲述了JS动态插入并立即执行回调函数的方法.分享给大家供大家参考,具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <
-
JS随机调用指定函数的方法
本文实例讲述了JS随机调用指定函数的方法.分享给大家供大家参考.具体如下: 本代码通过随机定时器调用指定函数,可达到间隔随机时间之行指定的函数的目的 function randRange(data) { var newTime = data[Math.floor(data.length * Math.random())]; return newTime; } function toggleSomething() { var timeArray = new Array(200, 300, 150,
-
centos 设置定时任务执行指定脚本的方法
vim /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of mont
随机推荐
- prototype 学习笔记整理
- AngularJS基础知识
- ListView 百分比进度条(delphi版)
- 在Ruby on Rails中使用Rails Active Resource的教程
- AngularJS ng-repeat遍历输出的用法
- Windows控制面板命令大全
- 浅谈javascript获取元素transform参数
- js代码延迟一定时间后执行一个函数的实例
- asp.net Cookie值中文乱码问题解决方法
- Apache服务器下防止图片盗链的办法
- PHP编译安装时常见错误解决办法
- PHP 数据结构队列(SplQueue)和优先队列(SplPriorityQueue)简单使用实例
- php数据库密码的找回的步骤
- CentOS安装mysql5.7 及简单配置教程详解
- 原生JS实现小小的音乐播放器
- jsp 获取客户端的浏览器和操作系统信息
- JavaWeb实现文件上传与下载实例详解
- 如何使用jquery动态加载js,css文件实现代码
- win2003 防止网卡本地连接被禁用的设置方法
- 告别Google补充材料的五个注意事项