SoFunction
Updated on 2024-11-17

python runserver process analysis

This article introduces the python runserver process analysis, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference learning value, the need for friends can refer to the following

releases

  • python27
  • django 1.0

Build a runnable environment

Creating a python27 virtual environment

github download django-1. (version 1.0 of django)

decompression (in digital technology)

As you can see, there is a demo in the examples directory

Copy the django directory under examples so that example can import django 1.0 correctly.

Initiation of projects

python  runserver

The project was launched successfully and the code can be modified to track the execution flow

workflows

The following code has been abridged, mainly to show the code flow

Starting at the beginning, the execute_manager method is executed, passing in the settings module.

execute_manager(settings)

.execute_manager method

def execute_manager(settings_mod, argv=None):
  The # setup_environ function, which just sets the environment variable, executes the configuration module
  # ['DJANGO_SETTINGS_MODULE'] = 
  setup_environ(settings_mod)

  # admin manage Tools
  utility = ManagementUtility(argv)
  ()

ManagementUtility Class

class ManagementUtility(object):
  def __init__(self, argv=None):
    # Initialization, e.g.
     = ['.../examples/', 'runserver']
    self.prog_name = ''
  def execute(self):
    # With some code removed, the final implementation of the code is roughly as follows
  
    # This is a command line tool class, what kind of parameters can the table name accept, here the main two parameters are checked
    # --settings Specify configuration file
    # --pythonpath Execute python environment variables
    parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
                 version=get_version(),
                 option_list=BaseCommand.option_list)
  
    # Parsing command line arguments using the command line utility class, i.e., getting the values of the --settings and --pythonpath arguments
    options, args = parser.parse_args()
    # Override the previously set ['DJANGO_SETTINGS_MODULE'] if the --settings parameter exists.
    # If the --pythonpath argument exists, the specified path will be added first, and the module will be loaded preferentially from there.
    handle_default_options(options)
  
    # fetch_command
    # fetch_command analysis below
    # fetch_command returns
    # run_from_argv
    # run_from_argv analyzed below
    self.fetch_command(subcommand).run_from_argv()
  
  def fetch_command(self, subcommand):
    # get_commands
    # get_commands Returns all modules in the directory, with each module handling the corresponding arguments
    # The value of each module is , app_name = ''
    app_name = get_commands()[subcommand]
  
    # load_command_class method
    # Returned
    klass = load_command_class(app_name, subcommand)
  
    return klass

run_from_argv method

#  
# Inherit import BaseCommand
# run_from_argv is also inherited
def run_from_argv(self, argv):
  # Call execute
  (*args, **options.__dict__)

def execute(self, *args, **options):
  # Call handle
  # Note that handle is overridden
  # The call is
  output = (*args, **options)

handle

def handle(self, addrport='', *args, **options):
  def inner_run():
    # WSGI Handler
    # WSGIHandler can be called and is a WSGI handler
    # AdminMediaHandler is a wrapper around WSGIHandler.
    # AdminMediaHandler Special handling of media file requests
    # AdminMediaHandler HTTP request for non-media files, returns WSGIHandler directly
    handler = AdminMediaHandler(WSGIHandler(), path)
    
    # 
    run(addr, int(port), handler)
    # run in
    # run is defined as follows
    # run starts an HTTP server, which can only be used for development and debugging.
    def run(addr, port, wsgi_handler):
      # Bind address port
      server_address = (addr, port)
      # Service instances
      httpd = WSGIServer(server_address, WSGIRequestHandler)
      # Incoming WSGI handler
      httpd.set_app(wsgi_handler)
      # Listening to requests
      httpd.serve_forever()
  
  inner_run()

This is the whole content of this article.