Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Running subs ,,in background''

by lRem (Scribe)
on Nov 23, 2005 at 20:18 UTC ( [id://511233]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings.
I have an app that collects some data every few seconds, and once about a minute draws graph of them. The problem is that drawing the graph sometimes takes enough time to lose a round of data collection. So I wanted to run the drawing sub asynchronously and decided, that this will be easier done with ithreads than with forks. As it doesn't need any communication with the main program after getting the argumets, the snippet looks real easy:
use threads; ... threads->create(\&draw, arg1, arg2, ...)->detach(); ...
But this creates problems. It happened to die with: *** glibc detected *** double free or corruption (fasttop): 0xb7e8d7d8 ***. It also sometimes happens, that the draw sub gets malformed args, what hasn't happened since months. But ultimately, even if everything ticks otherwise, after executing draw a few times the program just exits without goodbye! Now I don't know, is it something fundamental that I've missed, or the draw function somehow isn't thread safe, or ufo decided to disturb me.
For further detail: in fact in some moments there can be as much as 35 drawing threads created in a short time, but it never really stood up to even ten. The draw function is RRDs::graph, and I'm using perl 5.8.6.
--
Someday, people who know how to use computers will rule over those who don't. And there will be a special name for them: secretaries. -Dilbert quote

Replies are listed 'Best First'.
Re: Running subs ,,in background''
by Anonymous Monk on Nov 23, 2005 at 23:18 UTC
    use Proc::Simple;
    $myproc = Proc::Simple->new(); # Create a new process object $myproc->start(\&draw, # Launch a perl subroutine $param1, ...); # with parameters
    For robust threading please look at POE
      This is exactly what I wanted and runs perfectly :) Thanks for the advice.
      --
      Someday, people who know how to use computers will rule over those who don't. And there will be a special name for them: secretaries. -Dilbert quote
Re: Running subs ,,in background''
by Perl Mouse (Chaplain) on Nov 23, 2005 at 20:54 UTC
    I'd rather put my eggs in the basket of the OSses fork than in Perl's implementation of threads. ;-) The advantage of forking over threading (beside a non-thread capable perl being faster) is that if problems like this are triggered by the drawing routine, all it can do is bring down the program that draws the graphs - you might miss a graph, but your collector will still run.

    Personally, I would neither use fork(), nor threads. I'd write two programs, a data collector, and a graph drawer.

    However, none of the above really helps with solving the glibc problem. Of that, I do not know the answer.

    Perl --((8:>*
      Writing 2 programs is the last resort, as I wanted to keep it in one file. On the other hand, I'm also not sure about forking. Maybe I'll just make it take an argumet whether run as a collector or grapher.
      --
      Someday, people who know how to use computers will rule over those who don't. And there will be a special name for them: secretaries. -Dilbert quote
Re: Running subs ,,in background''
by Old_Gray_Bear (Bishop) on Nov 23, 2005 at 20:51 UTC
    If I Recall Correctly, RRDS::graphs is a wrapper around a call to rrdtool(). rrdtool() may not be thread-safe, or one of the C libraries that it uses isn't thread-safe. If it was me, I'd fall back and fork processes to build my graphs.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Running subs ,,in background''
by Moriarty (Abbot) on Nov 24, 2005 at 00:04 UTC

    I don't know anything about threads or forks, but I was curious as to why you would have more than one drawing thread. I would have thought that you would have a single drawing thread that sleeps for a minute at a time, waking up only to redraw the graph based on information that the main program had collected, then go back to sleep again.

      Because it doesn't draw just one graph of the data. In fact, it draws 35 graphs, but some of them are refreshed realy rarely. I could make it one thread taking a lot of communication, but I wanted to keep the source simple.
      --
      Someday, people who know how to use computers will rule over those who don't. And there will be a special name for them: secretaries. -Dilbert quote
Re: Running subs ,,in background''
by GrandFather (Saint) on Nov 23, 2005 at 20:48 UTC

    What OS are you using?


    DWIM is Perl's answer to Gödel
Re: Running subs ,,in background''
by crenz (Priest) on Nov 30, 2005 at 16:04 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://511233]
Approved by Old_Gray_Bear
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (None)
    As of 2024-04-25 00:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found