Suppose that I've written a wsgi application
. I run this application on Apache2
on Linux
with multi-threaded mod-wsgi
configuration, so that my application is run in many threads per single process:
WSGIDaemonProcess mysite processes=3 threads=2 display-name=mod_wsgi
WSGIProcessGroup mysite
WSGIScriptAlias / /some/path/wsgi.py
The application code is:
def application(environ, start_response):
from foo import racer
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [racer()] #call to racer creates a race condition?
module foo.py:
a = 1
def racer():
global a
a = a + 1
return str(a)
Did I just create a race condition with variable a
? I guess, a
is a module-level variable, that exists in foo.py
and is the same (shared) among threads?
More theoretical questions derived from this:
- Concurrent threads within the same process access and modify the same
a
variable so my example is not thread-safe? - If my web-server is
Apache
, each thread of my application on Linux is created on C-level withpthreads
API and the function, which thepthread
must execute is some kind of python interpreter's main function? Or does Apache protect me somehow from this error? - What if I were running this on a python-written web-server like
Tornado
'sHTTPServer
? Web server, written in python, implements threads as python-levelthreading.Thread
objects, and runsapplication
function in each thread. So, I suppose it's a race condition? (I also suppose, in this case I can abstract from underlying C-levelpthreads
belowthreading.Thread
implementation and worry only about python functions, because the interpreter won't allow me to modify C-level shared data and screw its functioning. So the only way to break thread-safety for me is to deal with global variables? Is that right?)