title: Sharing variables across multiple mod_wsgi processes/threads
body: <p>Can you have an object shared across multiple WSGI threads/processes (and in a manner that would work on both *NIX and Windows)?</p>
  
  <p>The basic premise:
  (1) I have a WSGI front end that will connect to a back end server. I have a serialization class, that contains rules on how to serialize/unserialize various objects, including classes specific to this project. As such, it needs to have some setup telling it how to handle custom objects. However, it is otherwise stateless - multiple threads can access it at the same time to serialize their data without issue.
  (2) There are sockets to connect to the back end. I'd rather have a socket pool than create/destroy every time there's a connection.</p>
  
  <p>Note: I don't mind a solution where there are multiple instances of (1) and (2), but ideally, I'd like them created/initialized as few times as possible. I'm not sure about the internals, but if a thread loops rather than closing and having the server reopen on a new request, it would be fine to have one data set per thread (and hence, the socket and serializer are initialized once per thread, but reused each subsequent call it handles.) Actually having one socket per thread, if that's how it works, would be best, since I wouldn't need a socket pool and have to deal with mutexes.</p>
  
  <p>Note: this is not sessions and has nothing to do with sessions. This shouldn't care who is making the call to the server. It's only about tweaking performance on systems that have slow thread creation, or a lot of memory but relatively slow CPUs.</p>
  
  <p>Edit 2: The below code will give some info on how your system shares variables. You'll need to load the page a few times to get some diagnostics...</p>
  
  <pre><code>from datetime import *;
  from threading import *;
  import thread;
  from cgi import escape;
  from os import getpid;
  
  count = 0;
  responses = [];
  lock = RLock();
  
  start_ident = "%08X::%08X" % (thread.get_ident(), getpid());
  
  show_env = False;
  
  def application(environ, start_response):
      status = '200 OK';
      this_ident = "%08X::%08X" % (thread.get_ident(), getpid());
  
  
      lock.acquire();
      current_response = """&lt;HR&gt;
  &lt;B&gt;Request Number&lt;/B&gt;: """ + str(count) + """&lt;BR&gt;
  &lt;B&gt;Request Time&lt;/B&gt;: """ + str(datetime.now()) + """&lt;BR&gt;
  &lt;B&gt;Current Thread&lt;/B&gt;: """ + this_ident  + """&lt;BR&gt;
  &lt;B&gt;Initializing Thread&lt;/B&gt;: """ + start_ident  + """&lt;BR&gt;
  Multithread/Multiprocess: """ + str(environ["wsgi.multithread"]) + "/" + str(environ["wsgi.multiprocess"]) +"""&lt;BR&gt;
  """
      global count;
      count += 1;
  
      global responses;
      responses.append(current_response)
      if(len(responses) &gt;= 100):
          responses = responses[1:];
      lock.release();
  
      output="&lt;HTML&gt;&lt;BODY&gt;";
  
      if(show_env):
          output+="&lt;H2&gt;Environment&lt;/H2&gt;&lt;TABLE&gt;&lt;TR&gt;&lt;TD&gt;Key&lt;/TD&gt;&lt;TD&gt;Value&lt;/TD&gt;&lt;/TR&gt;";
          for k in environ.keys():
              output += "&lt;TR&gt;&lt;TD&gt;"+escape(k)+"&lt;/TD&gt;&lt;TD&gt;"+escape(str(environ[k]))+"&lt;/TD&gt;&lt;/TR&gt;";
          output+="&lt;/TABLE&gt;";
      output += "&lt;H2&gt;Response History&lt;/H2&gt;";
      for r in responses:
          output += r;
      output+="&lt;/BODY&gt;&lt;/HTML&gt;"
  
  
      response_headers = [('Content-type', 'text/html'),
                          ('Content-Length', str(len(output)))]
      start_response(status, response_headers)
      return [output]
  </code></pre>