Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Polling a file without using huge amount of system resources

Reply
Thread Tools

Polling a file without using huge amount of system resources

 
 
Ishmael
Guest
Posts: n/a
 
      03-04-2009
I would like to check a file continuously to see if something has been
written to it. If so, immediately run a script. I can do this
successfully by repeatedly polling the file, as shown below, but this
uses up lots of system resources. I can, of course, add a 'sleep' to
the while loop, which reduces the drag on the system, but it also
slows down the response time.

Can someone recommend a replacement for this code using some more
elegant means? Ideally, I would like to set some sort of interrupt as
soon as the \n character is written to the file. That way, I can
avoid doing any polling myself and let the system handle it. Maybe
select(2) or FIFOs would work, but I've read the documentation and
can't figure out if this is really what I want. Thanks for your help!

# ---- My naive way ----
# Listener

open(FID, ">myfile") || die("Can't open: $!");
while (1) {
if (my $line = <FID>) {
print "$line\n";
last;
}
}


# Writer
open(FID, ">myfile") || die("Can't open: $!");
print FID "test\n";
 
Reply With Quote
 
 
 
 
Ishmael
Guest
Posts: n/a
 
      03-05-2009
Thanks for the response, that's quite helpful! As it turns out, FIFOs
do the trick for what I wanted. But now I'm curious - when I open,
for example, an xterm window, it will wait patiently until I type
something, and it knows (instantly!) when I press 'Enter'. That means
that somewhere, it is polling rapidly for keypress events, yet it uses
essentially no system resources. Do you know if that is implemented
using the functions you describe (like inotify?)? Or is there some
other secret mechanism?
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-05-2009
On Wed, 4 Mar 2009 19:05:01 -0800 (PST) Ishmael <(E-Mail Removed)> wrote:

I> But now I'm curious - when I open, for example, an xterm window, it
I> will wait patiently until I type something, and it knows (instantly!)
I> when I press 'Enter'. That means that somewhere, it is polling
I> rapidly for keypress events, yet it uses essentially no system
I> resources.

Actually X applications set up an event loop, where they respond to
messages from X. So for an xterm in particular, when you just start it,
the key press is delivered by a X event and then fed to whatever process
the xterm is running (e.g. a shell).

I> Do you know if that is implemented using the functions you describe
I> (like inotify?)? Or is there some other secret mechanism?

Look at IO::Select for a simple way to poll multiple filehandles (either
IO::Handle objects or file descriptors). The examples show how to use
it to check if input is waiting on STDIN, with \*STDIN.

Writing software in a time-sharing environment boils down to either
receiving events or yielding (via sleep/usleep) when there is nothing to
do. Usually, events are not available in the Unix environment unless
you implement a X event loop or something similar. So waiting for input
and yielding are the most common choice, and the one that works in
general terms with file or interactive input.

The interactivity of the application in a time-sharing environment
actually *increases* when it idles instead of consuming key presses
immediately. The reason is that the OS, meanwhile, can get a chance to
do other tasks instead of interfering with the application while the key
presses are handled. Just don't use sleep(1), that's too long, use
usleep(50) which is usually good enough for interactive use.

Anyhow, if you want good responsiveness in Perl programs, use
Term::ReadKey for single-key presses (it will tell you if a key press is
waiting) and Term::ReadLine::Gnu for line-based editing. Don't try to
be too clever with handling input, because you never know if you're
running in a local xterm or in a remote shell over a 9600 baud modem.

I'm sure much more can be said on this topic...

Ted
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count huge amount of files in folder MichiMichi ASP .Net 2 03-14-2007 07:39 PM
Trouble with huge amount of State Server Sessions Timed out =?Utf-8?B?RGFuaWVsIFdhbHplbmJhY2g=?= ASP .Net 7 09-28-2006 03:36 PM
Huge amount of invisible rubbish in C:\WINDOWS\Local Settings\Temporary Internet Files nemo Computer Support 33 03-22-2005 03:45 AM
Clicking a "mailto:" link on a page opens huge amount of IE windows after a while Henk Jol Firefox 1 01-04-2005 10:13 PM
Efficient format for huge amount of data Gabriel Genellina Java 21 01-23-2004 10:56 PM



Advertisments