@amjithr
$ time python fib.py
python fib.py 5.43s user 0.01s system 99% cpu 5.451 total
$ time python -m cProfile fib.py
python -m cProfile fib.py 9.63s user 0.04s system 99% cpu 9.670 total
Wednesday, May 15, 13
Slide 14
Slide 14 text
@amjithr
$ time python fib.py
python fib.py 5.43s user 0.01s system 99% cpu 5.451 total
$ time python -m cProfile fib.py
python -m cProfile fib.py 9.63s user 0.04s system 99% cpu 9.670 total
~ % OVERHEAD
Wednesday, May 15, 13
Slide 15
Slide 15 text
@amjithr
You can’t run it in production
Wednesday, May 15, 13
Slide 16
Slide 16 text
@amjithr
PROFILER IN PRODUCTION
Wednesday, May 15, 13
Slide 17
Slide 17 text
@amjithr
SLOW
PRODUCTION
NOT REALISTIC
cProfile
Wednesday, May 15, 13
@amjithr
SIGNALS
THREADS
INTERRUPT
Wednesday, May 15, 13
Slide 34
Slide 34 text
@amjithr
HTTP://FLIC.KR/P/bE NXH
SIGNALS
Wednesday, May 15, 13
Slide 35
Slide 35 text
@amjithr
import signal
# Use the virtual timer
sig = signal.ITIMER_VIRTUAL
# register a handler
signal.signal(sig, handler)
# 100ms repeating timer
signal.setitimer(sig, 0.1, 0.1)
SIGNAL
Wednesday, May 15, 13
Slide 36
Slide 36 text
@amjithr
PROs CONs
Accurate timer Unix only
- Ignored in mod_wsgi
-
Can’t interrupt
C-extensions
SIGNAL
Wednesday, May 15, 13
Slide 37
Slide 37 text
@amjithr
HTTP://FLIC.KR/P/db ZS
THREADS
Wednesday, May 15, 13
Slide 38
Slide 38 text
@amjithr
import threading
# Create a background thread
t = threading.Thread(target=handler)
t.setDaemon(True)
# Start the thread
t.start()
# Sleep for 100ms in handler
time.sleep(0.1)
THREAD
Wednesday, May 15, 13
Slide 39
Slide 39 text
@amjithr
PROs CONs
Cross Platform
Inaccurate for CPU
tasks
mod_wsgi compatible
Can’t interrupt
C-extensions
THREAD
Wednesday, May 15, 13
Slide 40
Slide 40 text
@amjithr
INQUIRE
Stack frame of every thread
Wednesday, May 15, 13
Slide 41
Slide 41 text
@amjithr
>>> import sys
# Get stack frames of every thread
>>> frames = sys._current_frames()
# Dictionary of { thread_id: frame_obj }
>>> frames
{4420: ,
1407: }
INQUIRE
Wednesday, May 15, 13
Slide 42
Slide 42 text
@amjithr
function3
function2
function1
FRAMES
Wednesday, May 15, 13
Slide 43
Slide 43 text
@amjithr
function3
function2
function1
Fn Name
File Name
Line No
Arguments
Globals
Locals
...
FRAMES
Wednesday, May 15, 13
Slide 44
Slide 44 text
@amjithr
function3
function2
function1
Fn Name
File Name
Line No
Arguments
Globals
Locals
...
FRAMES
Wednesday, May 15, 13
Slide 45
Slide 45 text
@amjithr
>>> import traceback
# Extract useful info from frame
>>> traceback.extract_stack(frame)
[('code.py', 88, 'runsource', 'return'),
('code.py', 110, 'runcode', 'print')]
EXTRACT
Wednesday, May 15, 13
Slide 46
Slide 46 text
@amjithr
COLLATE
Wednesday, May 15, 13
Slide 47
Slide 47 text
@amjithr
Default Dictionary
ATTEMPT
Wednesday, May 15, 13
Slide 48
Slide 48 text
@amjithr
Default Dictionary
Uniquely identify a
function
fdata = (f_name, file, line)
Build a dictionary calls = defaultdict(int)
Increment the value
when found in
frames
calls[fn_a] += 1
ATTEMPT
Wednesday, May 15, 13
Slide 49
Slide 49 text
@amjithr
Default Dictionary
Uniquely identify a
function
fdata = (f_name, file, line)
Build a dictionary calls = defaultdict(int)
Increment the value
when found in
frames
calls[fn_a] += 1
ATTEMPT
Wednesday, May 15, 13
Slide 50
Slide 50 text
@amjithr
Default Dictionary
Uniquely identify a
function
fdata = (f_name, file, line)
Build a dictionary calls = defaultdict(int)
Increment the value
when found in
frames
calls[fn_a] += 1
ATTEMPT
Wednesday, May 15, 13