The print_callers and print_callees method belong to the stats class of pstats module.
print_callers():This method for the Stats class prints a list of all functions that called each function in the profiled database. The ordering is identical to that provided by print_stats(), and the definition of the restricting argument is also identical. Each caller is reported on its own line. The format differs slightly depending on the profiler that produced the stats.
In cProfile each caller is preceded by three numbers: the number of times this specific
call was made, and the total and cumulative times spent in the current function
while it was invoked by this specific caller.
SYNTAX
print_callers(*restrictions)
print_callees():This method for the Stats class prints a list of all function that were called by
the indicated function. Aside from this reversal of direction
of calls (re: called vs was called by), the arguments and ordering are
identical to the print_callers() method.
SYNTAX
print_callees(*restrictions)
EXAMPLE
def Message():
for i in range(5):
print("List of Numbers:")
def Numbers():
list =[]
for i in range(0,20000):
list.append(i)
def main():
Message()
Numbers()
if __name__ == '__main__':
import cProfile, pstats
pr = cProfile.Profile()
pr.enable()
main()
pr.disable()
Sort_stats = pstats.Stats(pr)
Sort_stats.print_callers(5)
Sort_stats.print_callees(5)
In this code we have defined two functions and then passed it to the main function
After Profiling we have used both print_callers() and print_callees() method to print the
statistics with the restriction of 5 so that it prints only the five most
significant lines
OUTPUT
In the output first the results are printed using print_callers() method and next the
results are printed using print_callees() method where we can observe that the direction
of the calls is reversed.
Comments