I am developing a project in which I have to store all the function that were called in each request-response cycle and store them. I do not need to store values of the variable, all I need to store is function that we were called with their parameters and their order in execution. I am using mongodb to store this trace.
Storing Python Program Execution Flow (Function call Flow)
224 views Asked by Aditya At
2
There are 2 answers
0
On
sys.settrace traces a function for debugging but can be modified for this problem. Maybe something like this -
import sys
def trace_calls(frame, event, arg):
if event != 'call':
return
co = frame.f_code
func_name = co.co_name
if func_name == 'write':
# Ignore write() calls from print statements
return
func_line_no = frame.f_lineno
func_filename = co.co_filename
caller = frame.f_back
caller_line_no = caller.f_lineno
caller_filename = caller.f_code.co_filename
print 'Call to %s on line %s of %s from line %s of %s' % \
(func_name, func_line_no, func_filename,
caller_line_no, caller_filename)
Also see profiling. It describes how often and for how long various parts of the program executed
You could use a function decorator for convenience.
Then decorate your function(s) to log.
Test call.
If you wanted to store the entries in MongoDB directly instead of first logging to the
loggingmodule you can replace thelogging.debugline with code that creates an entry in your database.