[mod_python] Thread safety issue in Apache 2 Threaded MPM

Graham Dumpleton graham.dumpleton at gmail.com
Sat Feb 14 06:28:26 EST 2009

2009/2/14 howard chen <howachen at gmail.com>:
> Hello,
> I am a newbie to mod_python...as I am from PHP...
> Here are the story...
> In PHP's world, they recommend to use Apache 1.x or 2.x in prefork
> mode, to avoid the potential thread safety problem...their reasons
> are...
> ------------------------
> PHP is glue. It is the glue used to build cool web applications by
> sticking dozens of 3rd-party libraries together and making it all
> appear as one coherent entity through an intuitive and easy to learn
> language interface. The flexibility and power of PHP relies on the
> stability and robustness of the underlying platform. It needs a
> working OS, a working web server and working 3rd-party libraries to
> glue together. When any of these stop working PHP needs ways to
> identify the problems and fix them quickly. When you make the
> underlying framework more complex by not having completely separate
> execution threads, completely separate memory segments and a strong
> sandbox for each request to play in, feet of clay are introduced into
> PHP's system....
> ------------------------

Which looks in part like an admission that they aren't capable of
writing thread safe code, nor trust developers of third party
extensions for PHP to write thread safe code, or even trust the users
of PHP to write thread safe code. :-)

The issue is probably more that the heritage of PHP is such that
originally it wasn't written with multithreading in mind and so the
code that implements PHP and perhaps the language itself doesn't lend
itself to writing mutithread safe code. Thus, perhaps more poor design
decisions than it being a hard problem to solve.

> So it is the same with mod_python?
> So should I recommended to use Apache 1.x to avoid the potential
> thread safety issue?
> (Yes I know it is very rare, maybe even 0.0000001%, but just want to confirm)

Unlike PHP, the Python core and extensions modules available for it
are pretty well all thread safe. There are a few exceptions, but they
aren't commonly used packages.

Thus, what it really comes down to is whether you yourself think you
are capable of writing thread safe code. FWIW, it is much easier to
write thread safe code in Python than it is many other languages. But
if you don't think you understand multi thread programming enough to
do it properly, then use Apache 2.X and prefork MPM.

Do not use Apache 1.X, the version of mod_python available for it is
very old and buggy.

Of course, if you are working on Windows you have no choice as Apache
is only available in a multithread configuration on that platform.


More information about the Mod_python mailing list