【python使用装饰器和线程限制函数执行时间的方法】教程文章相关的互联网学习教程文章

python 写一个计算执行时间的装饰器

面试题之一。写一个装饰器wraps,它接收一个参数t,如果函数执行时间超过1秒,输出“bad”,否则输出“goods”。首先,计算函数的执行时间:import timestart = time.clock() for i in range(1000000): pass end = time.clock()print "cost time = %f s" % (end-start)结果:>>> cost time = 0.092749 s >>> 然后:import timedef warps(t): def deco(func): def _deco(*args,**kwargs): start = tim...

python限制函数执行时间【代码】

from:https://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python当有些函数执行时间过长,影响整个程序运行时,可以使用此方法进行限制,超时会报错。from__future__import with_statement # Required in 2.5import signal from contextlib import contextmanagerclass TimeoutException(Exception): pass@contextmanager def time_limit(seconds):def signal_handler(signum, frame)...

Python实现监控程序执行时间并将其写入日志的方法

本文实例讲述了Python实现监控程序执行时间并将其写入日志的方法。分享给大家供大家参考。具体实现方法如下:# /usr/bin/python # -*- coding:utf-8 -*- from time import time def logged(when):def log(f,*args,**kargs):print called:functions:%sargs: %rkargs: %r % (f,args,kargs)def pre_logged(f):def wrapper(*args,**kargs):log(f,*args,**kargs)return f(*args,**kargs)return wrapperdef post_logged(f):def wrapper(...

python – 服务器日志和我的客户端之间的SOAP调用执行时间测量差异【代码】

我正在为特定的SOAP API编写客户端.我的执行时间很长,因此联系了API所有者,他告诉我:The average duration calls of GetPrices calls calculated from twosources which store the duration of the calls show a average durationthroughout the 5 days that punter was calling our system of just over25 millseconds which matches the average of most punters during thesame time period . The two sources are the Sentry ...

使用Python执行时间的计算三种方法代码

这篇文章主要介绍了Python执行时间的计算方法小结的相关资料,需要的朋友可以参考下首先说一下我遇到的坑,生产上遇到的问题,我调度Python脚本执行并监控这个进程,python脚本运行时间远远大于python脚本中自己统计的程序执行时间。监控python脚本执行的时间是36个小时,而python脚本中统计自己执行的时间是4个小时左右。问题暴漏之后首先想到的是Linux出了问题,查找各种日志未发现有何异常。然后是想到python中用到的py2neo的写数...

python使用装饰器和线程限制函数执行时间的方法

本文实例讲述了python使用装饰器和线程限制函数执行时间的方法。分享给大家供大家参考。具体分析如下: 很多时候函数内部包含了一些不可预知的事情,比如调用其它软件,从网络抓取信息,可能某个函数会卡在某个地方不动态,这段代码可以用来限制函数的执行时间,只需要在函数的上方添加一个装饰器,timelimited(2)就可以限定函数必须在2秒内执行完成,如果执行完成则返回函数正常的返回值,如果执行超时则会抛出错误信息。# -*- co...

Python:执行时间【代码】

我写了一个小脚本来获取即时股票价格.#script to get stock datafrom __future__ import print_function import urllib import lxml.html from datetime import datetime import sys import timestocks=["stock1","stock2","stock3","stock4","stock5"]while True:f=open('./out.txt', 'a+')for x in stock:url = "http://someurltofetchdata/"+xcode = urllib.urlopen(url).read()html = lxml.html.fromstring(code)result = ...

python2和python3之间的执行时间略有不同【代码】

最后,我在python中编写了一个简单的排列生成器(Knuth在“The Art … 4”中描述的“普通变化”算法的实现).我很好奇python2和python3之间的执行时间差异.这是我的功能:def perms(s):s = tuple(s)N = len(s)if N <= 1:yield s[:]raise StopIteration()for x in perms(s[1:]):for i in range(0,N):yield x[:i] + (s[0],) + x[i:]在python3版本中,我刚将print x更改为print(x),因为print是py3中的函数.我使用timeit模块测试了两者. 我...

python – 为什么边界框搜索执行时间太长?【代码】

我正在使用GeoModel进行边界框搜索.数据存储区中有大约350个事件,但执行时间> 5秒.我的开发环境和谷歌应用引擎上的结果相同.为什么这么久?我需要添加一些索引吗?results = Event.bounding_box_fetch(Event.all(),geotypes.Box(north_east_lat, north_east_lng, south_west_lat, south_west_lng),max_results=100)在这个example它的效果要好得多.我正在使用Python 2.7和谷歌应用引擎解决方法:GeoModel现在是一种“老东西”.自新的...

如何限制python版Google App Engine中特定方法的允许执行时间?【代码】

由于python版本的Google App Engine不支持信号模块,如果方法在不到2秒内没有返回,调用方法和抛出/捕获异常的最简单方法是什么?解决方法:如果您正在讨论RPC调用(例如数据存储区),则可以使用截止时间创建RPC(请参阅create_rpc),将RPC传递给数据存储区功能(db.get,db.put等),然后捕获DeadlineExceededErrors.# Set a five-second timeout rpc = db.create_rpc(deadline=5)# A query: query = YourModel.all().fetch(100, rpc=rpc)URL...