I'm working on a tool to visualize Python profile traces, and to do that, it would be useful to know what line a function is called from.
Suppose I have the following simple program:
import time
def my_sleep(x):
time.sleep(x)
def main():
my_sleep(1)
my_sleep(2)
main()
If I run cProfile on it (python -m cProfile -o a.data a.py
), I get a marshaled file containing the following data structure:
('a.py', 3, 'my_sleep'): (2,
2,
1.4999999999999999e-05,
3.00576,
{('a.py', 6, 'main'): (2,
2,
1.4999999999999999e-05,
3.00576)}),
Essentially, this says that my_sleep()
is called twice by main()
on line 6, and this adds up to slightly over 3 seconds.
Is there any way to find out what line it's being called from, and not just what function? So in this case, my_sleep()
is called on line 7 for 1 second, and line 8 for 2 seconds.