Home > front end >  Is there a way to pass options values during mod_wsgi server start up in Django app
Is there a way to pass options values during mod_wsgi server start up in Django app

Time:02-11

I am using mod_wsgi to run my Django app. As there are a plethora of options to define when the server run command is fired, I was trying to create some kind of python script to pass the options and their pre-set values.

For example:

Instead of using:

$python3 manage.py runmodwsgi --processes 3 --threads 1

I am trying to use a python script and use it as:

$python3 runme.py

where runme.py is the script file I am trying to use to start the server.

What I have done so far?

Created the script file:

import os
from django.core.management import execute_from_command_line

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<proj>.settings')
myargs = 'runmodwsgi'
list_of_args = ['', myargs]
execute_from_command_line(list_of_args)

As expected the server started with preset options, for example:

enter image description here

Now what I am trying to achieve is the pass values of certain options like:

--processes 3 --threads 1

and so on.

Is there a way I may pass the preset values (as I may be able to define in my script file runme.py), say something like adding to the list of arguments:

list_of_args = ['', myargs, addl_args]

I have been checking SO queries posted in addition to help available on python site, but could not get my head around to the problem.

I tried the following which is not very helpful though:

import os
import argparse # New import
from django.core.management import execute_from_command_line

# Addition of new code lines
parser = argparse.ArgumentParser()

group = parser.add_mutually_exclusive_group()
group.add_argument("-p", "--processes", action="store_true")
group.add_argument("-t", "--threads", action="store_true")
parser.add_argument("pint", type=int, help="no. of processes")
parser.add_argument("tint", type=int, help="no. of threads")

args = parser.parse_args()
# End of new code

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<proj>.settings')
myargs = 'runmodwsgi'

# At this line if I add ", args" (like shown below:])
list_of_args = ['', myargs, args] 
execute_from_command_line(list_of_args)

and run the command, it comes up with error:

TypeError: 'Namespace' object does not support indexing

If I simply run: python3 runme.py, I get the following error:

usage: runme.py [-h] [-p | -t] pint tint
runme.py: error: the following arguments are required: pint, tint

Whereas, using

python3 runme.py 3 1

starts the server but the options integers "3" and "1" does not have any effect (as intended for no. of processes and threads).

If I use:

python3 runme.py --processes 3 --threads 1

I get the following error:

usage: runme.py [-h] [-p | -t] pint tint
runme.py: error: argument -t/--threads: not allowed with argument -p/--processes

Tried with a single arg like:

python3 runme.py --processes 3 1

The server starts at this time but the options value/s are not affected.

How do I define the option values and pass these preset values of options to the run command?

CodePudding user response:

It looks like you're just misunderstanding/misusing argparse.

import os
import sys
import argparse

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<proj>.settings')
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--processes", type=int, default=1)
parser.add_argument("-t", "--threads", type=int, default=1)
args = parser.parse_args()
from django.core.management import execute_from_command_line

command = [sys.argv[0], 'runmodwsgi', '--processes', str(args.processes), '--threads', str(args.threads)]
execute_from_command_line(command)

might be closer to what you want; it will default processes and threads both to 1.

  • Related