[mod_python] Re: forks, daemons and other beasts

Daniel Nogradi nogradi at gmail.com
Sat Feb 11 20:13:47 EST 2006

> > users upload a zip file and should get an acknowledgement
> > that the upload was successful. The server should start
> > unpacking the archive, check if the files are okay and
> > move them to a directory. All of this can take quite long,
> > so the acknowledgement should be sent before this
> > unpacking/checking/moving finishes.
> > The suggestion I gave wouldn't preclude an acknowledgement
> > being sent before the unpacking begins.
> >
> > Have you simply considered using a separate thread to do the
> > unpacking?
> > ...
> > Using threads you wouldn't need to do an os.fork(). Simply
> > create a new thread using the Python "threading" module
> > inside the Apache process to do the unpacking. You probably
> > just need to detach the thread so it finishes up properly
> > and doesn't need another thread to wait on it to finish.
> If you're using a multi-threaded MPM, you might not even want to detach it.
> It can be useful to let Apache return a "server unavailable" (503) response
> if all of your threads are busy unzipping files. If you're using a
> multi-process MPM, you might just do it in the same process to achieve the
> same effect.
> The thread or process won't die just because the client closes the
> connection. Right away, I'd give them the option: "Thanks for the file! It's
> being unzipped and processed now. You can trust me and close your browser,
> or wait for a final confirmation message when I'm done." Then, when the long
> process is done, write out the "final confirmation message" whether they're
> connected or not--Apache won't choke on that if they've left.

This sounds exactly what I would need, unfortunately I don't quite get
it. I tried this with the publisher handler:

def x( ):
    f = open( "/tmp/test", "w" )
    for i in range(1,10000):
        for j in range(1,10000):
            f.write( "%d %d\n" % ( i , j ) )

def test( req ):
    thread.start_new_thread( x, ( ) )
    time.sleep( 2 )
    return "okay"

And after accessing test( ) from a client the file /tmp/test will only
contain as many numbers as can be written in 2 seconds before reaching
return "okay". So when the main thread finishes, the new thread also
dies. Or did I misunderstand something?

More information about the Mod_python mailing list