python 3.7 的运行时实用程序 API 替代品 | Google 应用引擎

Runtime Utilities API alternative for python 3.7 | Google App Engine

我正在将现有的 py 2.7 存储库迁移到目前正在 Google App Engine 上工作的 py 3.7。

我发现一个runtime库(Runtime Utilities API)在项目中被广泛使用

from google.appengine.api.runtime import runtime
import logging

logging.info(runtime.memory_usage())

这将输出内存使用统计信息,其中数字以MB 表示。例如:

current: 464.0859375
average1m: 464
average10m: 379.575

我正在尝试寻找与 python 3.7 兼容的替代库,但没有从 GAE 中找到任何库。有人可以帮忙吗? Google 方面是否有我不知道的替代品?

不幸的是,google.appengine.api.runtime.runtime 模块 is deprecated 从版本 1.8.1 开始。

我也找不到 Python3 的任何类似或等效的官方 App Engine API。

作为替代方案,您可以尝试仅在代码中实现这些功能。例如,查看 this question, which is relate on how to Get RAM & CPU stats with Python. Some of them include the use of the psutil library.

的答案

您也可以考虑使用 StackDriver Agent,它可以将此页面上列出的指标类型的数据传输到 Stackdriver;例如 CPU(负载、使用情况等)、磁盘(使用的字节数、io_time 等)和其他指标。

以下获取与仪表板显示的内存使用值完全相同的值:

def current():
    vm = psutil.virtual_memory()
    return (vm.active + vm.inactive + vm.buffers) / 1024 ** 2

如果想要最小化转换成本,那么可以将以下内容放入新模块中,并导入而不是 Google 原始接口:

import psutil
class MemoryUsage:
    def __init__(self):
        pass

    @staticmethod
    def current():
        vm = psutil.virtual_memory()
        return (vm.active + vm.inactive + vm.buffers) / 1024 ** 2

def memory_usage():
    return MemoryUsage()