如何在 google 云 运行 上仅 运行 生产中的某些代码行?

How to only run certain lines of code in production on google cloud run?

我正在编写 FastAPI 服务并部署到 Google 云 运行。

我想 运行 我的笔记本电脑上的某些代码,云上的某些代码 运行,等等。 我想根据 运行 中的环境自定义代码。

我怎么知道我是否在容器中,google 云 运行,我的 Mac 笔记本电脑,vscode 空间,GitHub 操作。 ..如何将该信息添加到调试变量?

    debug,cloud,laptop,vcode = Environment()
    if debug:
        app.mount("/public", StaticFiles(directory="/public"), name="static")
    else:
        app.mount("/public", StaticFiles(directory="/debitcard/debitcard/public/static"), name="static")
    }

我如何知道 python 中的代码是否确实 运行ning 在我的笔记本电脑或云 运行 环境中?

答案是简单地在代码将 运行 的所有环境中设置环境变量。

在我的笔记本电脑上,我会这样做:

 export DEBUG=True
 export LAPTOP=True
 export CLOUDRUN=False
 export GITHUBACTIONS=False

在 Google Cloud 运行 上,我也会设置环境变量。 以及 GitHub 操作....