InviteBox / django-live-profiler

A database access profiler for Django-based applications that can be ran in production
193 stars 44 forks source link

Signal only works in main thread #13

Open clime opened 11 years ago

clime commented 11 years ago

I am getting the following exception when trying to run my app.

ValueError at /
signal only works in main thread

/srv/www/envs/cwu/lib/python2.7/site-packages/statprof.py in stop
       260.  signal.signal(signal.SIGPROF, signal.SIG_IGN) 

If I remove 'profiler.middleware.StatProfMiddleware', from MIDDLEWARE_CLASSES, the problem disappears but I have installed django-live-profiler specifically for python profiling.

I run the test server (./manage.py runserver). Any help, please?

atereshkin commented 11 years ago

It's a known issue but apparently not documented anywhere. Sorry about that. The workaround is to run the development server in single-threaded mode:

$ python manage.py runserver --nothreading --noreload

I am leaving this open to update the documentation.

clime commented 11 years ago

Thanks! That helped. I am getting another exception when accessing http://127.0.0.1:8000/profiler/:

EDIT: It sorted out after I restarted the testserver and python profiling works now!

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/profiler/

Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'south',
 'profiler',
 'grappelli',
 'django.contrib.gis',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'endless_pagination',
 'debug_toolbar',
 'anycluster',
 'web')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'profiler.middleware.ProfilerMiddleware',
 'profiler.middleware.StatProfMiddleware',
 'middleware.ProfilerMiddleware')

Traceback:
File "/srv/www/envs/cwu/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  92.                     response = middleware_method(request)
File "/srv/www/envs/cwu/src/django-live-profiler/profiler/middleware.py" in process_request
  34.         statprof.reset(getattr(settings, 'LIVEPROFILER_STATPROF_FREQUENCY', 100))
File "/srv/www/envs/cwu/lib/python2.7/site-packages/statprof.py" in reset
  270.     assert state.profile_level == 0, "Can't reset() while statprof is running"

Exception Type: AssertionError at /profiler/
Exception Value: Can't reset() while statprof is running
eressler commented 11 years ago

I'm getting this same error even with --nothreading and --noreload specified. Python 2.7.4, Django 1.5.1, latest django-live-profiler from git commit.

andrewhodel commented 10 years ago

http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Igetexceptions.ValueError:signalonlyworksinmainthreadwhenItrytorunmyTwistedprogramWhatswrong

tahseen09 commented 5 years ago

--noreload works for me but is there a permanent solution?

pirate commented 5 years ago

There doesn't appear to be a solution to the threading issue, since statprof cannot trigger a signal from a child thread.

However, I've updated this library to work on Django 2.2 and fixed a few other issues here, you may find this PR useful: https://github.com/InviteBox/django-live-profiler/pull/28