Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Google App Engine run one instance of an app per one request? or for all requests?

Using google app engine:

# more code ahead not shown
application = webapp.WSGIApplication([('/', Home)],
                                 debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

If two different users request the webpage on two different machine, two individual instances of the server will be invoked?

Or just one instance of the server is running all the time which handle all the requests?

How about if one user open the webpage twice in the same browser?

Edit:

According to the answers below, one instance may handle requests from different users turn by turn. Then consider the following fraction of code, taken from the example Google gave:

class User(db.Model):
    email = db.EmailProperty()
    nickname = db.StringProperty()

1, email and nickname here are defined as class variables? 2, All the requests handled by the same instance of server share the same variables and thus by mistake interfere with each other? (Say, one's email appears in another's page)

ps. I know that I should read the manual and doc more and I am doing it, however answers from experienced programmer will really help me understand faster and more through, thanks

like image 276
zhanwu Avatar asked May 05 '11 14:05

zhanwu


1 Answers

An instance can handle many requests over its lifetime. In the python runtime's threading model, each instance can only handle a single request at any given time. If 2 requests arrive at the same time they might be handled one after the other by a single instance, or a second instance might be spawned to handle the request.


EDIT:

In general, variables used by each request will be scoped to a RequestHandler instance's .get() or .post() method, and thus can't "leak" into other requests. You should be careful about using global variables in your scripts, as these will be cached in the instance and would be shared between requests. Don't use globals without knowing exactly why you want to (which is good advice for any application, for that matter), and you'll be fine.

like image 84
Wooble Avatar answered Oct 06 '22 00:10

Wooble