1024programmer Java djangoos.environ Use setdefault with caution to operate environment variables

djangoos.environ Use setdefault with caution to operate environment variables

In most cases, if you need to set environment variables while the program is running, there is no problem using the os.environ.setdefault function. However, there are two scenarios where setdefault will May cause unexpected problems, please use with caution:

If an environment variable (such as ENV=VAL1) already exists in the system before the program is executed, and if you use the setdefault function in the program to set a different value (such as VAL2) for the environment variable, the setdefault error will occur. The characteristics of the function prevent it from being set to a new value
It is also because of the above point that if process A first sets an environment variable (such as ENV=VAL1), and A starts child process B, child process B will inherit the value of process A. All environment variables will cause the environment variable ENV to already exist in the program running environment when B is running. As a result, if the setdefault function is used to set another different value (such as VAL2) for the environment variable at this time, the same reason will occur. As a result, it cannot be set to a new value
Therefore, the safest way to set system environment variables while the program is running is:

os.environ[‘ENV‘] = ‘VAL‘

Python obtains system environment variables os.environ and os.putenv
Start with a piece of code “if “BATCH_CONFIG_INI” in os.environ:” to determine whether the value of the environment variable is defined

If defined, get the value of the environment variable, otherwise get the config.ini file in the current directory.

if “BATCH_CONFIG_INI” in os.environ:
print “Using custom ini file!”
self.inifile = os.environ[“BATCH_CONFIG_INI”]
else:
self.inifile = self.cur_file_dir() + “/config.ini”
self.db_print (“inifile = (%s)” %(self.inifile))

How to set or get environment variables using Python Shell:

1. Set system environment variables

1. os.environ[‘environment variable name‘]=‘environment variable value‘ #where key and value are both string types

2. os.putenv(‘environment variable name‘, ‘environment variable value‘)

2. Obtain system environment variables

1. os.environ[‘environment variable name‘]

2. os.getenv(‘environment variable name‘)

The file main to create the client in celery:

from celery import Celery

import os
# Configure celery. If you need to use the configuration file, go there to load it
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “settings.dev”)

# 1. Create celery client
celery_app = Celery(‘‘)

# 2. Load configuration information
celery_app.config_from_object(‘celery_tasks.config’)
# 3. Register asynchronous tasks (those tasks can enter the task queue)
celery_app.autodiscover_tasks( [‘celery_tasks.sms’, ‘celery_tasks.email’, ‘celery_tasks.html’])

Django os.environ uses setdefault to operate environment variables with caution


This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/772395

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索