Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

mod_perl, long running process and broken connections

by glwtta (Hermit)
on Dec 15, 2003 at 19:52 UTC ( #314889=perlquestion: print w/replies, xml ) Need Help??

glwtta has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Ok, here is my problem: Under mod_perl (running Apache::Registry), I have a script that generates and image and sends it as the output. The image is generated by a third-party module (more specifcially, GraphViz), and can sometimes take a very long time to run (without any really good way to predict when).

Now from the application point of view it would be perfectly fine to just hit Stop on the browser if the image is taking too long and go on about your business (in other words, it's not all that essential for it to complete), of course if a user does that, then the neato process spawned by graphviz will just sit there, eating up cycles (often indefinitely). After a while I'll get enough of the little buggers that all the CPUs are continously busy.

So, what to do? I understand that I can use Apache->request->connection->aborted to check if the user has terminated the session, but that only helps if I am in some sort of loop where I can do the actual check. As it is, I am sitting waiting for GraphViz to do it's thing. I have a vague idea about forking off the process and then having the parent sit in a loop over the connection->aborted check and kill the child (must computer terminology be so morbid?) if it detects a broken connection. But I just don't know enough about IPC to know exactly what to do here.

Any suggestions? Thanks in advance.

  • Comment on mod_perl, long running process and broken connections

Replies are listed 'Best First'.
Re: mod_perl, long running process and broken connections
by Zaxo (Archbishop) on Dec 15, 2003 at 20:02 UTC

    If you fork off the graphics rendering and watch in the parent, as you are considering, it is very easy to knock off the child with,

    kill 'INT', $cpid; waitpid $cpid;
    You set $cpid from the return value of fork or open in the parent.

    After Compline,

Re: mod_perl, long running process and broken connections
by pg (Canon) on Dec 15, 2003 at 20:03 UTC
    • Spawn a new process for each instance of that graph generating thing you are doing;
    • In the parent process, keep an hash, push child’s process id there as key, and the connection as value.
    • The parent process periodically checks each child process in that table.
      • If the connection for a child is aborted, kill the child process, and remove the record from hash
      • also check whether the child process completed, if yes, remove the record from the hash.

    Personally, I would also push the start time of each child in the hash, so that I can check the elapsed time for each child process, and the parent process may kill the child process after an arbitrary time (your program has to prepare for the worst, what if the child process is just hanging?)

Re: mod_perl, long running process and broken connections
by edoc (Chaplain) on Dec 15, 2003 at 22:18 UTC
Re: mod_perl, long running process and broken connections
by TVSET (Chaplain) on Dec 15, 2003 at 22:57 UTC
    Another possible solution (apart from using fork as recommended by other monks) could be cache. You can pre-generate an an image once per some period of time and then show it instantly to other clients. It is even possible to use a script to make the request for image generation (something along the lines of WWW::Mechanize.

      If you are creating the same images repeatedly then this is definitely the way to go. If you put your image creation routine behind a 404 error handler and name your images so that you can parse out the details required to create the image, then you simply reference the image in your web page. If the image already exists it is served up, otherwise it is created, saved, and returned as normal. Also this way, the worst the user gets is a broken image if the request (for the image) times out.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://314889]
Approved by sunadmn
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-01-29 05:29 GMT
Find Nodes?
    Voting Booth?