http://qs321.pair.com?node_id=218889

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

I'm reading in some config_variables from a file which can be updated via a separate process (not a child), and I want to be able to notify my current process when this file gets written to, so that I can read in the newly changed config_variables.

I know that I can poll the file from an endless loop, but I was wondering if there was something neater, possibly with filehandles?

hoping against all hopes...

Replies are listed 'Best First'.
Re: How can I alert perl to a file write
by tachyon (Chancellor) on Dec 10, 2002 at 20:40 UTC

    So you want one process to pay attention to what a completely independent process is dong while the 'watching process' keeps its back turned and eyes closed so as to speak. As the processes are independent you will have to poll/signal somehow or other. Your options would include

  • writing the watching process pid to a file and having the updating process send a signal to it when it does the update.
  • forking off a child from the watching process to do guard duty.
  • Polling an ENV var/ lockfile / database periodically from the watching process where the updating process leaves its mark
  • Polling the file update time for the config file.
  • Alternatively you could just get your watcher to re-read the config file every X minutes and just accept that updates will be sub dynamic. If the config is small this will have next to no overhead and is probably the simplest to implement (ask Microsoft :0)
  • Finally the updating process could outright kill the watcher and fork off a new re-initialized one

    All of these approaches are used. There are no doubt others.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: How can I alert perl to a file write
by BrowserUk (Patriarch) on Dec 10, 2002 at 21:06 UTC

    If your on Windows take a look at Win32::Semaphore for a simple IPC signalling mechanism if you can modify the other process, or Win32::ChangeNotify to have the OS inform you when the file is modifed regardless of who modifies it.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: How can I alert perl to a file write
by hiseldl (Priest) on Dec 10, 2002 at 21:17 UTC

    The easiest way I can think of is to use the Msg.pm module from Advanced Perl Programming (it's in the Networking directory).

    In your running process you would put the listener code (ok, so it's more of a server), i.e.:

    use Msg; use strict; my $host = 'localhost'; my $port = 8080; Msg->new_server($host, $port, \&login_proc); print "Server created. Waiting for events"; Msg->event_loop(); #--------------------------------------------------------------- sub login_proc { # Unconditionally accept an incoming connection request return \&rcvd_msg_from_client; } sub rcvd_msg_from_client { my ($conn, $msg, $err) = @_; if (defined $msg) { print "$msg\n"; } }

    From the text, "The script calls new_server to create a listening socket (the program's network address) and then calls event_loop, an event dispatcher, which is a thin wrapper over select."

    And the client code that would send a message to your running process (server):

    use Msg; $conn = Msg->connect('localhost', 8080); die "Error: Could not connect\n" unless $conn; $conn->send_now("Message $i"); ($msg, $err) = $conn->rcv_now();

    Use the server and client scripts from the book's example code because the code here is for illustrative purposes.

    --
    hiseldl
    What time is it? It's Camel Time!

Re: How can I alert perl to a file write
by dingus (Friar) on Dec 10, 2002 at 20:49 UTC
    I think you will still have to poll something in a loop though...
    How about checking the mtime attribute to stat(filename)?
    # could do stat()[9] and not use File::stat use File::stat qw(:FIELDS); $cfgfile = '/my/config/variables.file'; stat($cfgfile) or die "No $cfgfile: $!"; my $last_mtime = $st_mtime; parse_config($cfgfile); #time passes #enter loop of things to do and events to check stat($cfgfile) or die "No $cfgfile: $!"; if ($last_mtime != $st_mtime) { $last_mtime = $st_mtime; parse_config($cfgfile); } # sleep(N) unless something else to do

    Dingus


    Enter any 47-digit prime number to continue.
Re: How can I alert perl to a file write
by tachekent (Acolyte) on Dec 10, 2002 at 20:46 UTC
    that's exactly what I needed to know.!
    Thank you very much.