title: Are global variables thread-safe in Flask? How do I share data between requests?
body: <p>In my application, the state of a common object is changed by making requests, and the response depends on the state.</p>
  <pre><code>class SomeObj():
      def __init__(self, param):
          self.param = param
      def query(self):
          self.param += 1
          return self.param
  
  global_obj = SomeObj(0)
  
  @app.route('/')
  def home():
      flash(global_obj.query())
      render_template('index.html')
  </code></pre>
  <p>If I run this on my development server, I expect to get 1, 2, 3 and so on.  If requests are made from 100 different clients simultaneously, can something go wrong? The expected result would be that the 100 different clients each see a unique number from 1 to 100. Or will something like this happen:</p>
  <ol>
  <li>Client 1 queries. <code>self.param</code> is incremented by 1.</li>
  <li>Before the return statement can be executed, the thread switches over to client 2. <code>self.param</code> is incremented again.</li>
  <li>The thread switches back to client 1, and the client is returned the number 2, say.</li>
  <li>Now the thread moves to client 2 and returns him/her the number 3.</li>
  </ol>
  <p>Since there were only two clients, the expected results were 1 and 2, not 2 and 3. A number was skipped.</p>
  <p>Will this actually happen as I scale up my application? What alternatives to a global variable should I look at?</p>