Opened 16 years ago

Closed 12 years ago

Last modified 11 years ago

#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 Eric Holscher, 16 years ago

Component: Uncategorizeddjango-admin.py runserver
milestone: post-1.0
Owner: changed from nobody to Eric Holscher
Status: newassigned

Known issue, but has to do with multi-threading in the auto_reload stuff.

comment:2 by Eric Holscher, 16 years ago

Triage Stage: UnreviewedSomeday/Maybe

comment:3 by Eric Holscher, 16 years ago

Owner: Eric Holscher removed
Status: assignednew

comment:4 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:5 by tiberiu_ichim, 15 years ago

Triage Stage: Someday/MaybeUnreviewed

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 tiberiu_ichim, 15 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 Chris Beaven, 15 years ago

Triage Stage: UnreviewedSomeday/Maybe

comment:8 by Gabriel Hurley, 14 years ago

Component: django-admin.py runserverCore (Management commands)

comment:9 by Luke Plant, 14 years ago

Severity: Normal
Type: Bug

comment:10 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:11 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:12 by Aymeric Augustin, 12 years ago

Resolution: needsinfo
Status: newclosed

Reading this ticket, I don't understand what the problem is, or what the fix should be.

comment:13 by Hipikat, 11 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? :)

Note: See TracTickets for help on using tickets.
Back to Top