Google App Engine实例生命周期 [英] Google App Engine Instance Life Cycle

查看:123
本文介绍了Google App Engine实例生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,
-
第一个实例维护一个计数变量(初始值为0),在服务10个请求后,计数增加到10.
然后,没有更多的请求,因此应用引擎关闭了第一例。

之后,创建第二个实例来处理传入的请求。

我的问题是计数变量将为10或重置为初始值(0)?

如果它被重置,是否有避免这种情况? (除了手动使用数据存储)?


谢谢

解决方案

我假设你正在谈论一个全局/模块级变量由每个请求递增。答案是每个实例都在自己的进程中运行,因此不仅后续实例将从零开始,而且由于您可以同时运行多个实例,所以每个实例都将拥有自己的独立计数器。



正确的做法是使用 memcache 来存储密钥,这将是所有实例中的单个值。您可以执行 memcache.incr('counter')以原子方式增加该值。



如果持续数据是重要的是,您可以考虑存储分片计数器


For examples,
First instance maintains a count-variable (init value is 0), after it served 10 requests, count increased to 10. Then there is no more request thus app engine shuts down the first instance.
After that, the second instance is created to serve incoming requests.
My question is the count-variable will be 10 or reseted to init value (0) ?
If it is reseted, is there anyway to avoid that ? (Beside using datastore manually )?
Thank you

解决方案

I presume you're talking about a global/module-level variable that is incremented by each request. The answer is that each instance runs in its own process, so not only will subsequent instances start at zero, but since you can have multiple instances running at the same time, each of them will have their own independent counters.

The proper way to do this is to use memcache to store the key, which will be a single value across all instances. You can do memcache.incr('counter') to increment the value atomically.

If persisting the data is important, you could look into storing sharded counters.

这篇关于Google App Engine实例生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆