Stay organized with collections
Save and categorize content based on your preferences.
Source code for google.appengine.api.runtime.runtime
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Runtime Utilities API.
.. deprecated:: 1.8.1
Utilities for interacting with the Python Runtime."""
from __future__ import with_statement
import threading
from google.appengine.api import apiproxy_stub_map
from google.appengine.api.system import system_service_pb
[docs]def cpu_usage():
"""Returns a SystemStat describing cpu usage, expressed in mcycles.
The returned object has the following accessors:
- total(): total mcycles consumed by this instance
- rate1m(): average mcycles consumed per second over the last minute
- rate10m(): average mcycles consumed per second over the last ten minutes
Functions for converting from mcycles to cpu-seconds are located in the quotas
API.
"""
return _GetSystemStats().cpu()
[docs]def memory_usage():
"""Returns a SystemStat describing memory usage, expressed in MB.
The returned object has the following accessors:
- current(): memory currently used by this instance
- average1m(): average memory use, over the last minute
- average10m(): average memory use, over the last ten minutes
"""
return _GetSystemStats().memory()
def _GetSystemStats():
"""Returns stats about the current instance."""
request = system_service_pb.GetSystemStatsRequest()
response = system_service_pb.GetSystemStatsResponse()
apiproxy_stub_map.MakeSyncCall('system', 'GetSystemStats', request, response)
return response
__shutdown_mutex = threading.Lock()
__shutdown_hook = None
__shuting_down = False
[docs]def is_shutting_down():
"""Returns true if the server is shutting down."""
with __shutdown_mutex:
shutting_down = __shuting_down
return shutting_down
[docs]def set_shutdown_hook(hook):
"""Registers a function to be called when the server is shutting down.
The shutdown hook will be called when the server shuts down. Your code
will have a short amount of time to save state and exit. The shutdown
hook should interrupt any long running code you have, e.g. by calling
apiproxy_stub_map.apiproxy.CancelApiCalls and/or raising an exception.
Args:
hook: A no-argument callable which will be called when the server is
shutting down.
Returns:
The previously registered shutdown hook, or None if no hook was
registered before.
In some cases it may not be possible to run the shutdown hook
before the server exits.
"""
if hook is not None and not callable(hook):
raise TypeError("hook must be callable, got %s" % hook.__class__)
global __shutdown_hook
with __shutdown_mutex:
old_hook = __shutdown_hook
__shutdown_hook = hook
return old_hook
def __BeginShutdown():
global __shuting_down
with __shutdown_mutex:
__shuting_down = True
shutdown_hook = __shutdown_hook
if shutdown_hook:
shutdown_hook()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["This Python source code provides utilities for interacting with the Google App Engine runtime environment, though it is marked as deprecated since version 1.8.1."],["The code offers functions to retrieve system statistics, such as CPU usage (in mcycles) and memory usage (in MB), including current and average usage metrics."],["It includes functionality to check if the server is currently in the process of shutting down using the `is_shutting_down()` method."],["A shutdown hook mechanism is provided via `set_shutdown_hook()`, allowing registration of a function to be executed during server shutdown, providing an opportunity for cleanup or state saving."]]],[]]