简体   繁体   中英

How to deal with environment variables and concurrency in Django

I'm working on a web application (using Django) that use another software to make some processing. This software needs to set its working directory to be in the environment variables. When a client make a request the app create the working directory (create data to be used by the external software). Then set the environment variable used by the external software to the created directory. Finally we call the external software and get the result.

Here's a summary of what the app is doing :

def request(data):
    path = create_working_directory(data)
    os.environ['WORKING_DIRECTORY'] = path
    result = call_the_external_software()

I haven't tested this yet (in reality it's not as simple as in this example). I'm thinking to execute this function in new process. Will I have problems when multiple client make simultaneous requests? If yes what should I do to fix the problems?

ps : I can't change anything on the external program.

See https://docs.python.org/2/library/subprocess.html#subprocess.Popen . Note that Popen takes a "env" argument that you can use to define environment variables in the child call.

def request(data):
    path = create_working_directory(data)
    env = {"WORKING_DIRECTORY": path}
    result = subprocess.call([ext_script] + ext_args, env=env)
    return result # presumably

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM