Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Last active December 18, 2015 13:59
Show Gist options
  • Select an option

  • Save kurtbrose/5794323 to your computer and use it in GitHub Desktop.

Select an option

Save kurtbrose/5794323 to your computer and use it in GitHub Desktop.
demonstration gevent and line_profiler playing nicely together (note: gevent.sleep(0) counts as all the time it spends waiting; if the primary case of sleeps is waiting for network or CPU bound tasks, this is valid; e.g. under high load this profiling will start to break down)
import gevent
import hashlib
import os
import line_profiler
profiler = line_profiler.LineProfiler()
@profiler
def task_one():
for i in range(100):
os.urandom(1024)
gevent.sleep(0)
def task_two():
for i in range(1000):
hashlib.md5(repr(i)).digest()
gevent.sleep(0)
if __name__ == "__main__":
gr1 = gevent.spawn(task_one)
gr2 = gevent.spawn(task_two)
gr1.join(30)
gr2.join(10)
profiler.print_stats()
$ python gevent_profile_test.py
Timer unit: 6.98413e-08 s
File: gevent_profile_test.py
Function: task_one at line 10
Total time: 0.0149639 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
10 @profiler
11 def task_one():
12 101 3157 31.3 1.5 for i in range(100):
13 100 97349 973.5 45.4 os.urandom(1024)
14 100 113750 1137.5 53.1 gevent.sleep(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment