Google App Engine单身人士(Python) [英] Google App Engine singletons (Python)

查看:116
本文介绍了Google App Engine单身人士(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中执行单例的标准方式是

pre code class Singleton(object):
_instance = None
def __new __(cls,* args,** kwargs):
如果不是cls._instance:
cls._instance = super(Singleton,cls).__ new __(cls,* args,** kwargs)
return cls._instance

但是,这不适用于App Engine,因为可能有很多服务器,并且我们每个服务器会得到一个实例。因此,我们如何为应用引擎实体做这件事?



类似于:

  class MySingleton(db.models):
def __init __(self):
all = MySingleton.all()
如果all.count()> 0:
return all.fetch(1).get()

super(MySingleton,self).__ init__(* args,** kwargs)
get()
调用

这会导致错误。 > __ init __



我们将如何使用它

我们只想表示一个配置文件,即:

  {'sitename':我的网站 ,'footer':This page owned by X} 


解决方案

如果你不打算将数据存储在数据存储中,为什么不用数据库创建一个模块而不是db.Model?



名称你的文件 mysettings.py 并在里面写:

  sitename =我的网站
footer =此页由X拥有

然后python模块有效成为一个单身人士。如果需要,您甚至可以添加功能。使用它,你可以这样做:

  import mysettings 
print mysettings.sitename

code>

这就是django用它们处理 DJANGO_SETTINGS_MODULE



更新
听起来您确实想使用db.Model,但使用memcached,因此您只能检索一个对象。但是当你改变数据时,你必须想出一个方法来刷新它,或者让它有一个超时,以便它偶尔得到。我可能会使用超时版本,并在mysettings.py中执行此类操作:

  from google.appengine.api import memcache $ b $ class MySettings(db.Model):
#属性...

def设置():
key =mysettings
obj = memcache.get(key)
如果obj是None:
obj = MySettings.all()。get()#假设只有一个
如果obj:
memcache.add (key,zone,360)
else:
logging.error(no MySettings found,create one!)
return obj

或者,如果您不想使用memcache,则只需将该对象存储在模块级变量中,并始终使用Settings()函数来引用它。但是你必须实现一种方法来刷新它,直到解释器实例被回收。我通常会使用memcached来实现这种功能。


The standard way of doing singletons in Python is

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        return cls._instance

However, this doesn't work on App Engine, since there are may be many servers and we would get one instance per server. So how would we do it for an app engine entity?

Something like:

class MySingleton(db.models):
    def __init__(self):
        all = MySingleton.all()
        if all.count() > 0:
             return all.fetch(1).get()

        super(MySingleton, self).__init__ (*args, **kwargs)

This leads to a recusion error, since get() calls __init__.

How we're going to use it:

We just want to represent a configuration file, ie:

{ 'sitename': "My site", 'footer': "This page owned by X"}

解决方案

If you aren't going to store the data in the datastore, why don't you just create a module with variables instead of a db.Model?

Name your file mysettings.py and inside it write:

sitename = "My site"
footer = "This page owned by X"

Then the python module effectively becomes a "singleton". You can even add functions, if needed. To use it, you do something like this:

import mysettings
print mysettings.sitename

That's how django deals with this with their DJANGO_SETTINGS_MODULE

Update: It sounds like you really want to use a db.Model, but use memcached so you only retrieve one object once. But you'll have to come up with a way to flush it when you change data, or have it have a timeout so that it gets get'd occasionally. I'd probably go with the timeout version and do something like this in mysettings.py:

from google.appengine.api import memcache
class MySettings(db.Model):
   # properties...

def Settings():
    key = "mysettings"
    obj = memcache.get(key)
    if obj is None:
       obj = MySettings.all().get()  # assume there is only one
       if obj:
            memcache.add(key, zone, 360)
       else:
            logging.error("no MySettings found, create one!")
    return obj

Or, if you don't want to use memcache, then just store the object in a module level variable and always use the Settings() function to reference it. But then you'll have to implement a way to flush it until the interpreter instance is recycled. I would normally use memcached for this sort of functionality.

这篇关于Google App Engine单身人士(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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