title: Global variable in Python server
body: <p>Background: I am a complete beginner when it comes to servers, but I know my way around programming in Python.</p>
  
  <p>I am trying to setup a simple server using the basic Python 2.7 modules (SimpleHTTPServer, CGIHTTPServer, etc). This server needs to load a global, read-only variable with several GB of data from a file when it starts; then, when each user accesses the page, the server uses the big data to generate some output which is then given to the user.</p>
  
  <p>For the sake of example, let's suppose I have a 4 GB file <code>names.txt</code> which contains all possible proper nouns of English:</p>
  
  <pre><code>Jack
  John
  Allison
  Richard
  ...
  </code></pre>
  
  <p>Let's suppose that my goal is to read the whole list of names into memory, and then choose 1 name at random from this big list of proper nouns. I am currently able to use Python's native CGIHTTPServer module to accomplish this. To start, I just run the CGIHTTPServer module directly, by executing from a terminal:</p>
  
  <pre><code>python -m CGIHTTPServer
  </code></pre>
  
  <p>Then, someone accesses <code>www.example-server.net:8000/foo.py</code> and they are given one of these names at random. I have the following code in <code>foo.py</code>:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import random
  
  name_list = list()
  FILE = open('names.txt','r')
  for line in FILE:
      name = line[:-1]
      name_list.append(name)
  
  FILE.close()
  name_to_return = random.choice(name_list)
  
  print "Content-type: text/html"
  print
  print "&lt;title&gt;Here is your name&lt;/title&gt;"
  print "&lt;p&gt;" + name_to_return + "&lt;/p&gt;"
  </code></pre>
  
  <p>This does what I want; however, it is extremely inefficient, because every access forces the server to re-read a 4 GB file.</p>
  
  <p>How can I make this into an efficient process, where the variable <code>name_list</code> is created as global immediately when the server starts, and each access only reads from that variable?</p>