3

While issuing management command runserver, it runs the server with loader. In source code of Django1.5 came across a piece of code where before staring a server in new process/thread , it specifically set the value of RUN_MAIN to 'true' in environment variable if its is not 'true'

django/utils/autoreload.py

 new_environ = os.environ.copy()
 new_environ["RUN_MAIN"] = 'true'
 exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)

In other piece of code it checks if that value value is being set ot not , if it is set then only it create a new process/thread.

 if os.environ.get("RUN_MAIN") == "true":
        thread.start_new_thread(main_func, args, kwargs)

by default in system(linux2) that value is not being set.

Queries:

1) what is the significance of that environment variable and how it is related to starting new process/thread.

2) if the "RUN_MAIN" is 'true' then code creates a thread, otherwise it creates a process. why is that ?

navyad
  • 3,752
  • 7
  • 47
  • 88

2 Answers2

1

Answer: 1) indeed, the enviroment parameter helps to manage many processes. 2) the reason is autoreload (when you save a file).

You will understand the process here:

http://programmersought.com/article/49721374106/;jsessionid=766C1F404087AC1E369350A887C32A21

dam_89
  • 11
  • 3
0

i view django code ,then i google your question,bug not answer.

i talk about my ideas.

first,inpython_reloader function, RUN_MAINis not define,so run restart_with_reloader function. in restart_with_reloader,create a child process, in child process RUN_MAIN is True. now, parent process waiting exit_code.

child process in python_reloader start a new thread for server. Next,child process's main thread run reloader_thread function ,when code file change , run sys.exit(3), child process quit, return exit code 3.

parent proces will run next loop,create other a child process, then reboot the server. if exit_code not eq 3(example, you press 'ctrl-c'), parent proces in restart_with_reloader function return exit_code, code run end.

my english is terrible,i hope you can understand it.

def reloader_thread():
    while RUN_RELOADER:
        # waiting code change
        if code_changed():
            sys.exit(3) # force reload

def restart_with_reloader():
    while True:
        new_environ = os.environ.copy()
        new_environ["RUN_MAIN"] = 'true'
        # create a child process
        exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
        if exit_code != 3:
            # end
            return exit_code

def python_reloader(main_func, args, kwargs):
    if os.environ.get("RUN_MAIN") == "true":
        # new thread in child process
        thread.start_new_thread(main_func, args, kwargs)
        try:
            reloader_thread()
        except KeyboardInterrupt:
            pass
    else:
        try:
            exit_code = restart_with_reloader()
            if exit_code < 0:
                os.kill(os.getpid(), -exit_code)
            else:
                sys.exit(exit_code)
        except KeyboardInterrupt:
            pass
0x55aa
  • 61
  • 3