Alternative to singleton?

I am new to Python and App Engine (and server!) And I am trying to create a very simple CMS. Each application deployment will have one - and only one - component object created from something like:

class Company(db.Model): name = db.StringPropery() profile = db.TextProperty() addr = db.TextProperty() 

I am trying to provide a tool for updating company profile and other details.

My first thought was that the company was Single Single. But having looked (though far from completely understood) this topic I get the impression that it is difficult and inappropriate to do this.

So, I thought that perhaps for every CMS deployment, I could run a script (called by an absolutely obscure URL) as a one-time, which just creates a company. From now on, I would get this instance with theCompany = Company.all()[0]

Is it possible?

Then I remembered that someone from this thread suggested just using a module. So I just created the Company.py file and fixed several variables in it. I tried this in the SDK and it seems to work - to my surprise, the changed values ​​of the variables "survived" between requests.

Forgive my ignorance, but I suppose these values ​​are only stored in memory, not on disks - like Datastore stuff? Is this a reliable solution? (And will the module variables have access to all the calls to my application scripts?)

+4
source share
3 answers

Global variables " application-cached ." This means that each specific instance of your application will remember the values ​​of these variables between requests. However, when the instance is turned off, these values ​​will be lost. Thus, I do not think that you really want to store these values ​​in the module level variables (unless they are constants that do not need to be updated).

I think your original solution will work fine. You can even create the source object using the remote API tool so that you do not need an obscure page to instantiate only the Company object.

You can also make retrieving a singleton Company object a little faster if you retrieve it with a key.

If you often need to query this object, you can avoid round trips to the data warehouse using the caching technique. The fastest way is to cache the Company object after you retrieve it from the data warehouse. To protect yourself from obsolescence, you can also cache the time during which you last retrieved the object, and if this time is more than N seconds, then you can retrieve it from the data store. For more information about this option and how to compare it with alternatives, check out Nick Johnson's article Storage Options in App Engine .

+2
source

It looks like you are trying to customize the way your application is configured for each application.

Why not use a data warehouse to store your company with a key? Then you will always know how to get a company, and you can edit the company without redeployment.

 company = Company(key_name='c') # set stuff on company.... company.put() # later in code... company = Company.get_by_key_name('c') 

Use memcache to save company information and avoid data warehouse recalls.

In addition to memcache, you can use module variables to cache values. They are cached , as you saw, between requests.

+2
source

I think the approach you are reading about is the simplest:

  • Use module variables initialized to None .
  • Provide accessors for these variables (get / setters).
  • When a variable is available, if its value is None, select it from the database. Otherwise, just use it.

This way you will have the app-wide variables provided by the module (which will not be repeated over and over), they will be available, and you will not lose them.

0
source

All Articles