0

I've got a project based on django, that wraps some custom code.

During import, this code is loading some heavy file to be executed. I need to check whether imports are executed under "runserver command" or not. This way I can prevent loading heavy files during django installation.

How can I check if code is executed under runserver command?

T3 H40
2,7288 gold badges44 silver badges55 bronze badges
asked Nov 10 at 9:55
4
  • 1
    Why not just set an environment variable and check that way? So MY_VAR=1 python manage.py runserver, then you can use sys.environ('MY_VAR) == '1'? Commented Nov 10 at 9:56
  • 3
    You'd probably be better off fixing the actual issue: you shouldn't do significant work when a file is imported, move the actual loading and computation inside some sort of function that you can call as needed. This will also be a problem for you in scenarios like unit testing, where you need the code available and may or may not want the expensive object load. Commented Nov 10 at 10:37
  • David, yes I agree, but in the project we use external library that by default has initialization with heavy files. Commented Nov 13 at 8:39
  • This question is similar to: How can I tell whether my Django application is running on development server or not?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 14 at 16:51

2 Answers 2

1

You can probably hack this into the handle of the runserver command, through monkey patching, i.e.:

# bad idea!!
GLOBAL_VAR = {
 'AS_RUNSERVER': False,
}
from django.core.management.commands.runserver import Command
old_handle = Command.handle
def new_handle(self, *args, **kwargs):
 GLOBAL_VAR['AS_RUNSERVER'] = True
 old_hande(self, *args, **kwargs)
Command.handle = new_handle

but I would advise to make this more clean, and work with an environment variable, like:

import sys
if os.environ.get('ENV_AS_RUN_SERVER') == '1':
 # ...

and then run this with:

ENV_AS_RUN_SERVER=1 python manage.py runserver
answered Nov 10 at 10:02
Sign up to request clarification or add additional context in comments.

Comments

1

You can override the runserver command to do whatever you want, it's a normal management command.

# your_app/management/commands/runserver.py
from django.conf import settings
from django.contrib.staticfiles.management.commands.runserver import Command as RunserverCommand
class Command(RunserverCommand):
 def add_arguments(self, parser):
 super().handle(self, parser)
 # you can add additional arguments here, maybe to decide whether you
 # should load your file or not
 def handle(self, *args, **kwargs):
 . . .
 # do special things here
 . . .
 super().handle(*args, **kwargs)

But you need to make sure the app housing this management command is installed before django.contrib.staticfiles in your INSTALLED_APPS setting.

INSTALLED_APPS = [
 . . .
 'your_app',
 . . .
 # load this after your_app so custom runserver command loads first
 'django.contrib.staticfiles',
]
answered Nov 10 at 14:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.