[mod_python] Re: Persistent socket connection.

StianSøiland stian at soiland.no
Tue Oct 28 00:44:38 EST 2003

On 2003-10-27 22:58:34, Gregory (Grisha) Trubetskoy wrote:

> > Upon creation of a new "session" I want to open a socket to a remote
> > server and be able to access this socket throughout the users session
> > until he logs our or the session is timed out.
> Given the multi-process nature of httpd, doing something like this would
> be very difficult.

I actually discussed this matter with my colleague tonight, if you can't
go multithreading, one solution is to have a middleware server running
on it's one.

The middleserver could use the asyncore module from Python to manage
multiple connections from a singlethreaded process. While containing
sockets to "the world" mod_python connects to the program by a local
UNIX socket or a longstanding TCP connection (to say, localhost)

The connection between mod_python processes, say one pr. process (ie. in
a module somewhere) - could last between different users and just say
some "Switch user"-command to the middleware.

A simple approach would be to associate those user socket sessions (like
a telnet session?) with the user by using his session ID. The mod_python
framework would only care about the session ID and formatting/passing
commands through in the right format.

The middleware could be smart or dumb - my suggestion is to make a
simple xml-rpc API on the middleware proxy, not just bypass the
socket communication. (why? To get short intervals to let other requests
to the single threaded proxy come through)


  * Middleware complexity increases risk of anything failing
    - severel connections could get lost 
    - the middleware could get trapped in a large respons/request
  * Scalabillity - the single-threaded middleware proxy could become the
    one thing slowing everything down in some situations - and 
    unless you've already thought of it, it could become difficult to
    scale up.

      simple workaround: 
        - several middleware instances - on the same or other machines
        - users are distributed to the most idle proxy


  * Go for threading and mod_python - it would make tasks
    much simpler. Just remember to have some locks (and timeouts) so two
    requests don't use the socket at once! (the double-reload syndrome)

  * If your environment requires a forking apache, why not use another
    apache installment on a local port? the "outer" apache could proxy
    requests down - just like wrapping away zope.

Stian Søiland               Work toward win-win situation. Win-lose
Trondheim, Norway           is where you win and the other lose.
http://www.soiland.no/      Lose-lose and lose-win are left as an
                            exercise to the reader.  [Limoncelli/Hogan]

More information about the Mod_python mailing list