1

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?

Edit/UPDATE:

Unlike this question: How can I tell whether my Django application is running on development server or not?, I don't need to know whether the server is in debug or production mode.

I need to "discriminate" some "imports" based on whether the command below

python manage.py <COMMAND> 

is runserver or not, regardless of whether it's debug or production mode.

5
  • 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
  • 1
    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
  • 1
    The linked question is not about debug or production mode, and most of the answers there directly answer the question of detecting whether the server is operating under runserver and/or doing conditional work based on that result, for example this one. Commented yesterday

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
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',
]

Comments

Your Answer

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.