启动和停止 Google App Engine 后端

发布于 2024-12-01 22:49:29 字数 122 浏览 2 评论 0原文

我阅读了 Google App Engine 后端文档,但我仍然无法理解如何从 Python 启动/停止后端(动态后端)(我猜是使用 URLFetch)。

有人能给我一个代码示例吗?后端不会使用应用程序的默认版本。

I read the Google App Engine backend docs, but I still can't understand how to start/stop backends (dynamic backends) from Python (using URLFetch, I guess).

Could someone give me a code example? The backend will not be on the application's default version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-12-08 22:49:29

这取决于您使用的后端类型,“常驻后端”只能通过管理控制台或命令行从生产环境中关闭,而“动态后端”会在闲置几分钟后关闭。

因此,如果您使用动态后端,您只需发送一个请求,告诉它停止正在执行的操作,它就会自动关闭。

http://code.google.com/intl /iw/appengine/docs/python/config/backends.html#Types_of_Backends

编辑

其工作原理示例:

from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext.webapp.util import run_wsgi_app
import time

class ShutdownHandler(webapp.RequestHandler):
    def get(self):
        memcache.put('backendShutdown', True, 60)

class StartHandler(webapp.RequestHandler):
    def get(self):
        lastCheck = time.time()
        while True:
            if time.time() - 60 > lastCheck:
                stopBackend = memcache.get('backendShutdown')
                if stopBackend:
                    memcache.delete('backendShutdown')
                    break
                lastCheck = time.time()


if __name__ == '__main__':
    _handlers = [(r'/_ah/start', StartHandler),
                 (r'/backend/worker/shutdown', ShutdownHandler)] # somekind of handler for shutdown
    run_wsgi_app(webapp.WSGIApplication(_handlers))

要停止它,您可以 使用:

from google.appengine.api import backends, urlfetch
url = backends.get_url('worker') + '/backend/worker/shutdown'
urlfetch.fetch(url)

It depends on what type of backend you are using, "Resident Backends" can't be shutdown from the production environment only via the Admin Console or command-line while "Dynamic Backends" are shutdown after sitting idle for a few minutes.

So if you use Dynamic Backends you can just send a request telling it to stop what it is doing and it will be shutdown automatically.

http://code.google.com/intl/iw/appengine/docs/python/config/backends.html#Types_of_Backends

Edit

Example of how this might work:

from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext.webapp.util import run_wsgi_app
import time

class ShutdownHandler(webapp.RequestHandler):
    def get(self):
        memcache.put('backendShutdown', True, 60)

class StartHandler(webapp.RequestHandler):
    def get(self):
        lastCheck = time.time()
        while True:
            if time.time() - 60 > lastCheck:
                stopBackend = memcache.get('backendShutdown')
                if stopBackend:
                    memcache.delete('backendShutdown')
                    break
                lastCheck = time.time()


if __name__ == '__main__':
    _handlers = [(r'/_ah/start', StartHandler),
                 (r'/backend/worker/shutdown', ShutdownHandler)] # somekind of handler for shutdown
    run_wsgi_app(webapp.WSGIApplication(_handlers))

And to stop it you would use:

from google.appengine.api import backends, urlfetch
url = backends.get_url('worker') + '/backend/worker/shutdown'
urlfetch.fetch(url)
浅听莫相离 2024-12-08 22:49:29

使用 appcfg 启动和停止后端。来自文档

appcfg 后端

;启动<后端>

将后端状态设置为 START,允许其接收 HTTP 请求。
常驻后端立即启动。动态后端不启动
直到第一个用户请求到达。如果后端是,则没有影响
已经开始了。

appcfg 后端

;停止<后端>

将后端状态设置为 STOP 并关闭所有正在运行的实例。
停止的后端无法接收HTTP请求;如果它收到
请求,它返回 404 响应。如果该命令无效
后端已经停止。

Use appcfg to start and stop backends. From the documentation:

appcfg backends <dir> start <backend>

Sets the backend state to START, allowing it to receive HTTP requests.
Resident backends start immediately. Dynamic backends do not start
until the first user request arrives. Has no effect if the backend was
already started.

appcfg backends <dir> stop <backend>

Sets the backend state to STOP and shuts down any running instances.
The stopped backend cannot receive HTTP requests; if it recevies a
request, it returns a 404 response. This command has no effect if the
backend was already stopped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文