Understanding global object persistence in Python WSGI apps

前端 未结 3 774
庸人自扰
庸人自扰 2021-02-05 20:01

Consider the following code in my WebApp2 application in Google App Engine:

count = 0

class MyHandler(webapp2.RequestHandler):

    def get(self):

        glob         


        
3条回答
  •  梦如初夏
    2021-02-05 20:32

    Your analysis of the situation is correct, a Python web app is a long-running process. It takes a long time to spin up the Python interpreter and is not done every request.

    It's entirely possible to create a global variable that is different "per-request". This is done in a lot of frameworks and people seem to like it. The way to do this does depend on the server. Most servers use "one thread per request", and I think GAE does as well. If this is the case you can use a threadlocal variable. If you are worried about this value sticking around between requests on that thread, you will need some management code that can hook to the start/end of a request. WSGI middleware is a good place for this if the WebApp2 framework doesn't provide a nice way to do it.

    It's just Python, and a request is served in its own thread. From there you can do what you'd like. There's nothing in Python to just reset all the global variables, and there's generally no guarantee (especially with GAE) that the process serving your request will be the same process everytime, meaning your globals shouldn't be used to persist data between requests unless you really know what you're doing.

    There are many frameworks out there that provide good support for doing this already, so if WebApp2 doesn't then I'd suggest looking elsewhere. Python has a lot of options and many of them do run on GAE.

提交回复
热议问题