0

I am somewhat confused over how Python for GAE works.

If I have a variable x, that is not in memcache and not in the db, does it retain it's value the next time the script runs (i.e., for the next user)?

class somePage(webapp2.RequestHandler):
    x = 1
    def get(self):
        if x == 2:
            foo()
        else:
            bar()
            x = 2

Will foo() ever get called?

What if x is declared outside of somePage?

x = 1
class anotherPage(webapp2.RequestHandler):
    def get(self):    
        if x == 2:
            foo()
        else:
            bar()
            x = 2

Is the script "fresh" every time it's hit, or is it like doing:

$ python
>>>

And then having every user queue up to bash (not intended) at the same keyboard?

7
  • I'm not sure about the title - is 'persistent' the appropriate word? Commented Jul 28, 2014 at 21:43
  • 4
    Mutable global variables are bad juju. Even if it works, GAE can run multiple instances of your program, or kill and start new ones at any time. Distinct instances will not share the value. Don't even think about it, this is not an acceptable way to share context. Commented Jul 28, 2014 at 21:44
  • Roger that, thank you. It seems memcache is the way to go. Commented Jul 28, 2014 at 21:46
  • 1
    I guess foo() will never be called. Commented Jul 28, 2014 at 21:52
  • 1
    Memcache + datastore backup in order to store the values is the best way to go -- remember that items can be evicted from memcache at any moment Commented Jul 28, 2014 at 23:55

1 Answer 1

1

tldr; the way GAE works, a global mutable variable is not a viable option for sharing context.

Like most fully-managed PaaS solutions, one of the main goals of GAE is automatic scalability. You can have more than one instance of your program running at the same time; the dispatcher can kill and/or start instances at any time and each one runs at completely isolated environments.

Due to the higher degree of isolation between the instances, sharing context through a global mutable variable is not a viable an option - it may seem to work sometimes, but you can't rely on that.

In order to share context there are a few options:

  • one of the GAE managed services, for example: Task Queues, Memcache or the Users API
  • one of their storage options: a traditional MySQL database using Cloud SQL, a schemaless NoSQL datastore, or object storage using Cloud Storage
  • connect to a 3rd party service offered on their marketplace

You can also run your own service using their IaaS offer (but this defeats some of the main reasons for using a PaaS).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.