什么是“绑定”的安全方式python中的环境变量? [英] What is a safe way to "bound" environment variable in python?
问题描述
A
女巫行为取决于一些环境变量的值。
A_CONFIG_PATH
实际上。我的一些任务使用这个库与每个任务不同的
A_CONFIG_PATH
。我以的方式执行
import os
import A
def task(** kw):
os.environ ['A_CONFIG_PATH'] ='/ home / me / current / task / config / path'
A.do_some_stuff(kw)
直到所有任务同步处理为止。但是现在我需要
并发在这个任务处理。
所以我如何保证每个任务不会损坏另一个
自己的A_CONFIG_PATH如果我运行每个任务在单独的线程/进程
或这样的东西。
有几种方法来处理问题:
- 在子进程中运行每个任务,而不是在不同线程中的所有进程
- 更改任务以将
A_CONFIG_PATH
值作为参数,而不是从环境中读取(从env读取与依赖于全局变量一样糟糕) li>
- 而不是将一个字符串分配给
os.environ [A_CONFIG_VALUE]
,使用threading.local
对象,它允许每个线程拥有自己的值。您需要稍微更改读取值的部分。
使用线程的示例。
#one time init
os.environ ['A_CONFIG_PATH'] = threading.local()
#设置值
def任务(** kw):
os.environ ['A_CONFIG_PATH']。value ='/ home / me /当前/任务/ config / path'
A.do_some_stuff(kw)
#读取值
config_path = os.environ ['A_CONFIG_PATH'] value
因为你说env var正在使用 os.getenv $ c读取, $ c>,你可以将第三个解决方案与这个hack结合起来,用你自己的代替 os.getenv
: #一次init
orig_getenv = os.getenv
def getenv_hacked(key,default = None):
如果键=='A_CONFIG_PATH':
返回orig_getenv(键,默认).value
else:
返回orig_getenv(键,默认)
os.getenv = get env_hacked
os.environ ['A_CONFIG_PATH'] = threading.local()
Suppose we have a library A
witch behavior depends on value of some
environment variable. A_CONFIG_PATH
actually. Some of my tasks use
this library with different A_CONFIG_PATH
for each task. I do it in a
way of
import os
import A
def task(**kw):
os.environ['A_CONFIG_PATH'] = '/home/me/current/task/config/path'
A.do_some_stuff(kw)
This fine until all tasks process synchronously. But now I need
concurrency in this tasks processing.
So how I can guarantee that each task will not corrupt another with
its own A_CONFIG_PATH if I run each task in separate thread/process
or something like this.
解决方案 There are several ways to tackle the problem:
- run each task in a subprocess, instead all in one process in different threads
- change the task to take the
A_CONFIG_PATH
value as a parameter, instead of reading it from environment (reading from env is just as bad as relying on global variables...)
- instead of assigning a string to
os.environ[A_CONFIG_VALUE]
, use a threading.local
object, which allows each thread to have its own value. You'd need to slightly change the part which reads the value, though.
An example using threading.local
:
#one time init
os.environ['A_CONFIG_PATH'] = threading.local()
# set the value
def task(**kw):
os.environ['A_CONFIG_PATH'].value = '/home/me/current/task/config/path'
A.do_some_stuff(kw)
# read the value
config_path = os.environ['A_CONFIG_PATH'].value
EDIT: since you said the env var is being read using os.getenv
, you can combine the third solution with this hack, replacing os.getenv
with your own:
# one time init
orig_getenv = os.getenv
def getenv_hacked(key, default=None):
if key == 'A_CONFIG_PATH':
return orig_getenv(key, default).value
else:
return orig_getenv(key, default)
os.getenv = getenv_hacked
os.environ['A_CONFIG_PATH'] = threading.local()
这篇关于什么是“绑定”的安全方式python中的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!