Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python twisted:检索延迟的';s执行时间_Python_Twisted_Deferred - Fatal编程技术网

python twisted:检索延迟的';s执行时间

python twisted:检索延迟的';s执行时间,python,twisted,deferred,Python,Twisted,Deferred,我想知道延迟执行需要多长时间,从启动第一个回调到最终结果 有没有关于如何做到这一点的想法,可能是以非侵入性的方式(意味着不修改任何回调函数来跟踪执行时间)?如果您在“twistd”的帮助下运行程序,那么它有一个选项--profile”,可以帮助您分析扭曲的代码 twistd "other options" --profile=statsfile --profiler=cProfile --savestats 要查看统计数据,请执行以下操作: import pstats stats = psta

我想知道延迟执行需要多长时间,从启动第一个回调到最终结果


有没有关于如何做到这一点的想法,可能是以非侵入性的方式(意味着不修改任何回调函数来跟踪执行时间)?

如果您在“twistd”的帮助下运行程序,那么它有一个选项--profile”,可以帮助您分析扭曲的代码

twistd "other options" --profile=statsfile --profiler=cProfile --savestats
要查看统计数据,请执行以下操作:

import pstats
stats = pstats.Stats('statsfile')
stats.sort_stats('time').print_stats()
在延迟触发后立即执行回调。但立即意味着延迟链中的每个回调都必须执行,它们将有自己的执行时间。此外,各种代码段都有自己的执行时间片,包括reactor循环

所以说“立即”这个词就是说“尽快”

考虑以下坏例子:

from twisted.internet import reactor, defer
import time

def timeit(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

d = defer.Deferred()

@timeit
def callfunc1(result):
    print 'XXXXX'

@timeit   
def callfunc2(result):
    print 'YYYYY'

d.addCallback(callfunc1)   
d.addCallback(callfunc2)  
t1 = time.time()
d.callback(True)
t2 = time.time()
print '%0.3f ms' % ((t2-t1)*1000.0)
输出:

XXXXX
callfunc1 took 0.039 ms
YYYYY
callfunc2 took 0.005 ms
0.108 ms

现在,如果我们调整上面的代码,使其包含一个reactor和callLater,那么对于我想要实现的目标来说,评测有点过头了

我最终得到了一个解决方案,它并不意味着对现有代码进行大量修改,但绝不是“通用的”:

我最初的代码是这样的:

def myfunc(*args):
    d = Deferred()
    d.addCallback(cb1)
    ...
    d.addCallback(lambda x: MyObject(x))
我现在有:

def myfunc(*args):
    init_time = time.time()
    d = Deferred()
    d.addCallback(cb1)
    ...
    d.addCallback(lambda x: MyObject(x, init_time))

class MyObject:
    def __init__(self, *args):
        ...
        self.exec_time = time.time() - init_time

它正是我想要的,但我希望延迟结构能够显示一些跟踪执行时间本身的东西,而不是必须修补我的对象。从源代码中,我看不出有这样的东西可用:

我想你应该配置你的应用程序,按照以下方式操作:

安装此工具

启动twisted应用程序并收集原始数据:

twistd--savestats-n--profile=myapp.hotshot-myapp

然后,将“hotshot”转换为“calltree”,运行:

hotshot2cg myapp.hotshot>myapp.calltree

现在我们可以在Kcachegrind工具中查看calltree

kcachegrind myapp.calltree

使用此工具,您可以查看twisted事件循环的调用图, 您可以看到以百分比为单位的执行时间。 因此,无需修补代码,只需运行此工具并查看

检查内存的注意事项:

@user304965:我不太愿意给出这样的答案,主要是因为您必须对回调进行分析。如果您正在编写自己的代码,这是可能的。然而,为了这个目的而编写代码是很难看的。但是,如果您使用的是来自其他模块的回调,那么您甚至不能这样做。这个想法是回调将立即启动,但这取决于许多事情的进展。什么提供了hotshot2cg命令?我在pip或brew中找不到任何关于它的内容,也没有在网上提及……hotshot2cg就像一个转换器,是KCacheGrind应用程序的一部分,所以应该先安装它。即使在安装了kcache grind之后,我也无法运行hotshot2cg。我无法找到任何将hotshot转换为calltree的算法/软件。Downvote命令是“hotshot2calltree”。无论如何,hotshot(格式)似乎过时了,被pstats所取代,这正是cProfile现在提供给您的。你可以把它转换成。