什么是“绑定”的安全方式python中的环境变量? [英] What is a safe way to "bound" environment variable in python?

查看:110
本文介绍了什么是“绑定”的安全方式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如果我运行每个任务在单独的线程/进程
或这样的东西。

解决方案

有几种方法来处理问题:


  1. 在子进程中运行每个任务,而不是在不同线程中的所有进程

  2. 更改任务以将 A_CONFIG_PATH 值作为参数,而不是从环境中读取(从env读取与依赖于全局变量一样糟糕) li>
  3. 而不是将一个字符串分配给 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 ,你可以将第三个解决方案与这个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:

  1. run each task in a subprocess, instead all in one process in different threads
  2. 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...)
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆