[英]How to change the value of an Environment variable from the code of Google Cloud Function?
I'm trying to understand how to use environment variables properly for Google Cloud Functions .我试图了解如何为Google Cloud Functions正确使用环境变量。
I've deployed a function and I want to write and read it's environment variable (eg: XXX
).我已经部署了一个 function 并且我想写入和读取它的环境变量(例如:
XXX
)。
if not os.environ.get("XXX"):
print("XXX_SET_DEFAULT")
os.environ.setdefault("XXX","1")
else:
print("XXX_SET")
os.environ["XXX"] = "1"
print("XXX_GET", os.environ["XXX"])
After triggering the function, the value initially set in the Runtime environment variables list does not change.触发function后,Runtime环境变量列表中初始设置的值没有变化。
Although the behavior is little bit different, because when I deploy the variable with the function with a dummy value I can see XXX_SET
string in the logs, but when I don't deploy the variable first, for the first run I can see XXX_SET_DEFAULT
, then for the second run there is XXX_SET
.虽然行为有点不同,因为当我使用虚拟值部署变量 function 时,我可以在日志中看到
XXX_SET
字符串,但是当我不先部署变量时,第一次运行时我可以看到XXX_SET_DEFAULT
,然后第二次运行有XXX_SET
。
I've set the variables with (and from the UI too):我已经设置了变量(也来自用户界面):
gcloud functions deploy my_func \
...
--set-env-vars XXX=xxx
Questions :问题:
XXX_SET_DEFAULT, XXX_SET
vs. XXX_SET, XXX_SET
)XXX_SET_DEFAULT, XXX_SET
与XXX_SET, XXX_SET
)Runtime environment variables
and Build environment variables
? Runtime environment variables
和Build environment variables
有什么区别?
Build environment variables
somehow?Build environment variables
之间部署变量吗? You are not meant to change environment variables in GCFs.您无意更改 GCF 中的环境变量。 They are there to set configurations.
他们在那里设置配置。
Here are some basics:以下是一些基础知识:
Build environment variables
are for when the container is built. Build environment variables
用于构建容器时。 Unless you are messing with how you build, you will not need these.Runtime environment variables
are for when the function runs in the container. Runtime environment variables
用于 function 在容器中运行时。 These are the ones you can access with your code. What this means, is every time you run a function, it'll get a new container, with all the variables set to whatever you've set them in configuration.这意味着,每次运行 function 时,它都会得到一个新容器,所有变量都设置为您在配置中设置的值。
Now, it is possible that GCF in the background reuses some of the containers, and you may see some persistance but you cannot rely on this.现在,后台的 GCF 可能会重用一些容器,你可能会看到一些持久性,但你不能依赖它。
In conclusion:综上所述:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.