more useful options | |
PerlMonks |
Re: Run and kill external programm after x secondsby davido (Cardinal) |
on Nov 22, 2016 at 14:40 UTC ( [id://1176340]=note: print w/replies, xml ) | Need Help?? |
You can set an alarm and then time-out after blocking for awhile. Or you could do your reads non-blocking, and iterate until time is up. With a little effort you can also continue doing other work while processing your input. The following example reads from a program that outputs "ping" every few seconds. But when there's no input to read, it does other things (outputs a dot). And when a "ping" is detected, it responds by outputting a "pong".
The ping script can look like this:
Using a poor-man's event loop (the while loop), and by taking care to not let the "handlers" block, this script is able to accept input and when there's no input spend time doing other things. When this script exits, the pipe closes and the ping script will receive a signal to terminate. This is written with Linux in mind. I don't know if it would work for Windows, and don't have a Windows environment to test on anymore. Dave
In Section
Seekers of Perl Wisdom
|
|