Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Managing an asynchronous neverending external program

by Porax (Acolyte)
on Jul 09, 2014 at 09:36 UTC ( [id://1092871]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I am really new to Perl and having some issues I don't really know how to handle. I work in a Windows environment.

I made a father script that loops to gather configuration information and provide data files to another program. No real problem here, this task was a good first contact with Perl (XML reading and writing, regex...).

My problem is that this program doesn't end on its own, it has to run for a certain duration(which I get in the conf files) so the standards functions like open(), exec(), or system() can't help here as I need to ask the program to stop (in a clean way) and those will just have my script start to wait. I could have set a timeout but the duration set in the files may change while the program is already running so I have to update it.

Also I need to start several instances of the external program as new conf files become available and all this may get even more complicated after the first prototype development phase as the instances of the external program will probably have to be distributed across several servers (it is a quite memory consuming app), this will be a problem to be adressed later but it may influence the choice here.

I have looked for ideas and found some modules that look like they could help me here but I feel a bit overwhelmed by all this information :p

- Win32::Job

- POE

- IPC::Cmd

Or I could make a "double fork", one to start the program, the other one to process its output files and close it when the time has come (I would need to find the right cmd window (probably thanks to its title) and emulate a click on the cross or a Ctrl-C to end it properly).

Which one of the above approaches seems the best suited fo my needs (and being reasonably easy to put to use for the confused beginner I am) ? Do you know other modules that could do the job? Do you have any general advice/examples for this situation ?

Thank you in advance for your wisdom !

Porax

  • Comment on Managing an asynchronous neverending external program

Replies are listed 'Best First'.
Re: Managing an asynchronous neverending external program
by zentara (Archbishop) on Jul 09, 2014 at 10:24 UTC
    Genaral advice: Use an Event-loop system, which runs on Windows, like AnyEvent or Glib . Your event loop will spawn the program, in a way to get it's PID. Your eventloop runs a timer, to see when that troublesome program has finished, and then sends a Kill signal to it. See Roll your own Event-loop for a simple example.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks a lot zentara !

      I just started having a look at AnyEvent and for what I have read so far it really seems to reach my needs !

      Porax

        Be aware that Perl on Windows has some rough edges and IPC and event-based programming tend to get into those edges the most!
Re: Managing an asynchronous neverending external program
by sam_bakki (Pilgrim) on Jul 10, 2014 at 08:18 UTC
Re: Managing an asynchronous neverending external program
by Porax (Acolyte) on Sep 01, 2014 at 09:06 UTC

    Hi all,

    Realising I didn't give any feedback !

    I finally managed to have it work the way I want using fork() and the Win32 API (Process, Console and Event).

    My main script forks to call a second "son" script which creates the event, starts a new console and then forks and calls exec of my external program in that console.

    I am then able to control the ending of the process from the main script.

    Thanks for pointing me in the right direction !

    Porax

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-16 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found