Python twisted: returns delayed runtime

I would like to know how long the delay will take, from the moment of the first appeal to the final result.

Any ideas on how to do this, possibly in a non-invasive manner (which means there are no changes for any callback function to track the execution time)?

+3
source share
3 answers

If you run your program using “twistd”, it has the “--profile” option, which can help you with profiling twisted code.

twistd "other options" --profile=statsfile --profiler=cProfile --savestats

And to view statistics:

import pstats
stats = pstats.Stats('statsfile')
stats.sort_stats('time').print_stats()

. , , . , .

- .

:

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

, , callLater

+7

, , :

http://kcachegrind.sourceforge.net/html/Home.html

:

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

, 'hotshot' 'calltree', :

hotshot2cg myapp.hotshot > myapp.calltree

calltree Kcachegrind.

kcachegrind myapp.calltree

, . ,

P.S. : guppy/heapy

+2

, .

, , "":

:

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

It does what I want, but I was hoping the Deferred structure would reveal something by tracking the execution time, rather than messing around with my objects. From the source code, I see that such a thing is not available: http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.0.0/twisted/internet/defer.py#L137

+1
source

Source: https://habr.com/ru/post/1771778/


All Articles