#8085 closed Bug (needsinfo)
call_command('runserver') executes management command twice.
| Reported by: | Eric Holscher | Owned by: | |
|---|---|---|---|
| Component: | Core (Management commands) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Someday/Maybe | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When you use runserver in a management command, it executes the code in the management command twice. This is cited in the testserver management command:
32 # Run the development server. Turn off auto-reloading because it causes
33 # a strange error -- it causes this handle() method to be called
34 # multiple times.
35 shutdown_message = '\nServer stopped.\nNote that the test database, %r, has not been deleted. You can explore it on your own.' % db_name
36 call_command('runserver', addrport=addrport, shutdown_message=shutdown_message, use_reloader=False)
I ran into this as well, and I'm just putting it in here so hopefully someone sees it and fixes it.
Change History (13)
comment:1 by , 17 years ago
| Component: | Uncategorized → django-admin.py runserver |
|---|---|
| milestone: | → post-1.0 |
| Owner: | changed from to |
| Status: | new → assigned |
comment:2 by , 17 years ago
| Triage Stage: | Unreviewed → Someday/Maybe |
|---|
comment:3 by , 17 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:5 by , 16 years ago
| Triage Stage: | Someday/Maybe → Unreviewed |
|---|
One issue I have related to this: the second time the code is executed, in my setup (I'm using zc.buildout, and I have Django listed as an egg dependency for my own egg), it can't find the django modules.
comment:6 by , 16 years ago
My problem is solved if I run "runserver" with the --noreload option, so the problem must be in django.utils.autoreloader.
Some details about my setup: my python is actually a script generated with zc.recipes.egg's "interpreter" option. This script inserts all my egg dependencies in the sys.path, then calls an execfile(mymodule). I think the autoreloader only looks at the module that was executed (manage.py) but ignores the special settings for sys.path that that module had.
comment:7 by , 16 years ago
| Triage Stage: | Unreviewed → Someday/Maybe |
|---|
comment:8 by , 15 years ago
| Component: | django-admin.py runserver → Core (Management commands) |
|---|
comment:9 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Bug |
comment:12 by , 13 years ago
| Resolution: | → needsinfo |
|---|---|
| Status: | new → closed |
Reading this ticket, I don't understand what the problem is, or what the fix should be.
comment:13 by , 12 years ago
Hi. I just encountered this, too…
The problem is just that the reloader starts news threads, and so… django.utils.autoreload.restart_with_reloader calls new_environ["RUN_MAIN"] = 'true' and then django.utils.autoreload.python_reloader knows to look, for if os.environ.get("RUN_MAIN") == "true": then it's in a spawned thread. The solution is just to check that RUN_MAIN is False in your handle method…
# some_app.management.commands.some_command.py
import os
from django.core.management.commands import runserver
class Command(runserver.Command):
def handle(self, *args, **options):
if not os.environ.get('RUN_MAIN', False):
# some_code
super(Command, self).handle(*args, **options)
Anyway, I'm guessing this is too esoteric and internal to justify a note anywhere in the main documentation, but I'm not going to be the last person to get stumped by it, so commenting on this issue seemed like the appropriate place to mention the solution? :)
Known issue, but has to do with multi-threading in the auto_reload stuff.