Kushal Das

FOSS and life. Kushal Das talks here.


mod_wsgi and a Python extention

I was working on a performance analysis of a web API. After I identified the possible issues, I tried to see if I can use a native extension for that part of code (it is a Flask application). In this case the extension was written in Rust. It worked very well. In both test environment and using mod_wsgi-express it was super fast. But it failed when I tried to use it in the production under nginx + mod_wsgi combination. The import modulename statement was just stuck. Causing a timeout in the application. There were no other error messages or log lines.

Found the cause only after having a chat with Graham. After listening to the problem, he told me the solution in seconds. To set the Application Group as %{GLOBAL}.

WSGIApplicationGroup %{GLOBAL}

This is to make sure that we are using the first (main) interpreter, instead of any of the sub-intepreters. The reason explained in the mod_wsgi documentation

The consequences of attempting to use a C extension module for Python which is implemented against the simplified API for GIL state management in any sub interpreter besides the first, is that the code is likely to deadlock or crash the process.