Skip to content

Commit

Permalink
Improve error reporting and choose correct profiler for middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
joerick committed Apr 7, 2014
1 parent 2edc0f4 commit 69e0d9d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 7 additions & 4 deletions pyinstrument/middleware.py
@@ -1,8 +1,7 @@
import os
from django.http import HttpResponse
from django.conf import settings
from pyinstrument import Profiler
from pyinstrument.stat_profiler import NotMainThreadError
from pyinstrument import StatProfiler, EventProfiler
from pyinstrument.stat_profiler import NotMainThreadError, SignalUnavailableError

not_main_thread_message = (
'pyinstrument can only be used on the main thread. Run your server process in single-threaded '
Expand All @@ -16,7 +15,11 @@ def __init__(self):

def process_request(self, request):
if getattr(settings, 'PYINSTRUMENT_URL_ARGUMENT', 'profile') in request.GET:
self.profiler = Profiler()
try:
self.profiler = StatProfiler()
except SignalUnavailableError:
self.profiler = EventProfiler()

try:
self.profiler.start()
except NotMainThreadError:
Expand Down
9 changes: 8 additions & 1 deletion pyinstrument/stat_profiler.py
Expand Up @@ -12,12 +12,19 @@ def __init__(self, message=''):
super(NotMainThreadError, self).__init__(message or NotMainThreadError.__doc__)


class SignalUnavailableError(Exception):
'''pyinstrument.StatProfiler uses signal.SIGALRM, which is not available on your system.
Consider using pyinstrument.EventProfiler instead.'''
def __init__(self, message=''):
super(SignalUnavailableError, self).__init__(message or SignalUnavailableError.__doc__)


class StatProfiler(BaseProfiler):
def __init__(self, *args, **kwargs):
try:
signal.SIGALRM
except AttributeError:
raise AttributeError('pyinstrument.StatProfiler uses signal.SIGALRM, which is not available on your system. Consider using pyinstrument.EventProfiler instead.')
raise SignalUnavailableError()

self.next_profile_time = 0
self.interval = 0.001
Expand Down

0 comments on commit 69e0d9d

Please sign in to comment.