GrahamDumpleton / mod_wsgi

Source code for Apache/mod_wsgi.
Apache License 2.0
1.02k stars 269 forks source link

Why can't Apache just run Python such it does with PHP? #904

Closed korpe1 closed 3 days ago

korpe1 commented 2 weeks ago

Hi,

I'm just curious, why can't Apache run Python scripts with a simple module like mod_php?

Django says following:

Django, being a web framework, needs a web server in order to operate. And since most web servers don’t natively speak Python, we need an interface to make that communication happen. The runserver command starts a lightweight development server, which is not suitable for production.

source: https://docs.djangoproject.com/en/5.1/howto/deployment/

I don't get that, what the language matter in this? Write a simple module that just put /usr/bin/python3 interpreter (actually kernel does it for you, just exec() ) to run file and read stdout.

And another question, why we need a standard protocol for Python programs? How Apache know how execute every PHP scripts, but Python needs dedicated solutions or a protocol.

Cheers, Korpe

GrahamDumpleton commented 2 weeks ago

Starting up a new instance of the Python interpreter for every web request is very inefficient and slow. It isn't practical except for the very simplest of Python web handlers. If you really want to do that then using mod_cgi.

PHP is no different to Python. It needs a bridge between Apache request handling and PHP code. This is what mod_php does. The mod_wsgi module is no different and hooks into Apache the same way that mod_php does, to provide a means of running Python code inside of the Apache processes. It just so happens that for Python they standardised on the API interface being very minimal WSGI since Python existed as a standalone language already. PHP instead was originally designed to only work with Apache, so its language APIs more or less were bound direct into Apache. It was only later that PHP normalised the API somewhat so it could exist separate to Apache and run in separate processes.

Note that before mod_wsgi existed there was also mod_python. It had APIs which mapped closer to underlying Apache APIs. It fell out of favour because your Python web application would only work with Apache. The WSGI interface that Python later adopted meant that it was possible to achieve portability, such that Python web applications could run on any web server supporting WSGI.

Did you also know that besides runserver, Django could only originally be deployed on Apache using mod_python. Django moving to using WSGI is the only reason you can now also run it with gunicorn, uWSGI and other Python web servers which support WSGI, as well as with mod_wsgi on Apache.