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

I'm using Proc::Background to spawn off command lines (that redirect append STDERR and STDOUT to log files) and then using the 'alive' method to determine when it is safe to spawn another. The problem I'm running into with this is the command initialization time.

I'd like to invoke the command once and then feed it a series of lines, each consisting of the command line arguments for that invocation -- sort of like a server. I'd prefer to keep something like the 'alive' method to determine when its safe to write the next line of arguments to invoke the new instance of the 'server'.

What (Meta)CPAN module(s) would be best?

  • Comment on Converting Proc::Background Tasks To Server

Replies are listed 'Best First'.
Re: Converting Proc::Background Tasks To Server
by sierpinski (Chaplain) on Dec 21, 2014 at 06:29 UTC
    I'm not exactly sure what you are asking. You mentioned a problem with command initialization time, but then you're asking for a suggestion on CPAN modules.

    What exactly are you spawning with the Proc::Background? A shell? Then you want to send more commands to it? I'm curious why you wouldn't just fork a new Proc::Background process with a new command. Perhaps if you gave a solid example of what you are trying to accomplish?

    Perhaps I'm just not understanding your question...
      A simplified example of the current code might be:
      while(1){ # a bunch of stuff $command = some calculated command; $args = some calculated args; $proc{"$command $args"} = Proc::Background->new("$command $args 1> +&2 >some.log") unless $proc{"$command $args"}->alive; # a bunch of stuff }
      In this example, $command is being spawned off and must bring up a new process every time. I'd rather just be able to, say, repeatedly write "$args 1>&2 >some.log\n" to a pipe and then have the new line fork off a process from a $command that has already been initialized -- with something analogous to "->alive" to tell me when it is ok to fork off another similar $command.
        Proc::Background has a wait function that you can use to get the status code of the child process, however...

        It sounds to me that Proc::Background isn't really suited for what you need. From reading the docs, it just seems like a wrapper for starting/killing/waiting on processes, so that's basically just fork, wait, etc. If you need to communicate with a running process, I think perlipc is better suited to your needs.