multithreading

francois lepoutre franck.lepoutre at caramail.com
Fri Feb 21 12:41:23 EST 2003


> assume I have a Python obj instance in a web application after
> Apache+modPython.
> The point is: can I assume that accessing local variables within a
> method of such instance is thread-safe, e.g. syncronized by the
> interpreter against concurrent invokations by multiple threads (e.g.
> multiple web clients) ?

> In a C++ environment, such automatic variables would be on the stack,
> e.g. they would exist per-thread. But in Python they are in the heap,
> so I dont't know about concurrency.

I have no system or C++ background but we use modpython extensively
(and so do our customers) for data-intensive stuff. And we like it.

I'll try to give you my understanding of  the issue. I may be wrong though.

We buffer a lot of info in python structures in order to keep our busy
db-based system speedy and efficient.

Enters the distinction between modpython/*nix and modpython/win32.

Apache/win32 is threaded. The modpython/win32 the programming model
is thread-based as well.

Typically you have ONE python interpeter running within apache.
Every call to the modpython stuff will trigger a thread.

Not simple as you need to protect any shared data structures by a lock.
And you may need to protect other resources such as odbc handlers
in some way as well.

On *nix, apache is typically forked. Under stress, modpython
will raise a bunch of python interperters.

Because there's no threading, your python structures are safe
but every of your python handler will hold its stuff.

It is way simpler and, of course it scales better. But you may
need to find a way to re-sync all your running processes in some way.

I understand that apache 2/*nix offers a mixed threaded-forked
model  as well. Looks like the best of both worlds.

But I have found no evidence of the way to get it to work.
It'd be great as it would make it possible  to use a unified
environment for both win32 and *nix.

Any feedback on this is welcome.

François








More information about the Python-list mailing list