One of the most important differences between using WSGI applications and ordinary CGI applications is that WSGI applications code is not loaded on each request. Instead it is loaded once and then repeatedly executed. This means that you cannot put information which is likely to change on each request in the global namespace because it will not be updated.
A good example to illustrate this problem is web.cgi
. The web.cgi
variable is loaded once and contains any CGI variables to be passed to a CGI script. Since each CGI request completely reloads and executes all code in the script, web.cgi
will always contain the correct CGI variables when used in a CGI script.
However, consider this example application:
import web def application(environ, start_response): start_response('200 OK', [('Content-type','text/plain')]) return ['Here are the CGI variables: %s'%('\n'.join(web.cgi.keys()))]
The first time the application is run, the correct results will be displayed, the second time it is run by the WSGI server, web will already have been imported and will not be imported again. This means the web.cgi
variable will be out of date.
The solution to this is to put everything which needs to be reloaded on each request into the main body of the application, or consider placing it as a middleware component.
import cgi def application(environ, start_response): start_response('200 OK', [('Content-type','text/plain')]) return ['Here are the CGI variables: %s'%('\n'.join(cgi.FieldStorage().keys()))]
XXX Probably need to explain this better.