Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl/TK looping 1

Status
Not open for further replies.

Rajoidea

IS-IT--Management
Jul 9, 2001
12
0
0
US
I'm having trouble making a Perl/TK gui update itself automatically. Shorthand, it should be able to sit active in the background and perform the job it was built for about once a mintue. Anyone know how to do this?

Tom Ragsdale
stingray@acl.lanl.gov
 
I think we need more details to solve this problem. Here's a few questions that I have:

What is the process and how is it running in the background?
Are you trying to update on an event that is happening or just update every minute?
Are there any other details or code snippets that you can provide?
 
The process is pinging every node in my cluster to verify they're up. In this case, there's no specific event to trigger the update (I put an "update now" function elsewhere), it should just happen each minute.

Here's a *highly* condensed (since I'm fairly certain that how I draw the gui isn't really the problem ;-) ) of what I'm doing:

Code:
use Tk;
use Net::Ping;  
use Getopt::Long;

#handle/parse command line stuff...

my $mw = MainWindow->new();

#draw the interface...

&pingnow; #This part works. It successfully pings everything
#once when it starts up.

MainLoop;

sub newrange  #Changes the number of nodes to ping
#newrange definition...

sub pingnow
{
   @Bad = ();
   $p = Net::Ping->new("$opt_p");
   foreach $k(@Hosts)
     {
      if (!($p->ping($k)))
        {
          push(@Bad, $k);
        }
     }
   $p->close();
   $badlist->delete(0, 'end'); #clear the old list of bad nodes
   $badlist->insert('end', @Bad); #dump in the new list
}

If seeing all the various widget->place commands would help, I can put those up as well, but like I said, I don't think how I draw it really impacts refreshing after it's drawn. I've tried putting refresh instructions in after Mainloop;, but no luck. If the Reader's Digest Condensed version above is too condensed, I'll be happy to email the whole of the source to anyone who needs it.

Tom Ragsdale
stingray@acl.lanl.gov
 
To perform the job every minute, add the following code right before the line 'MainLoop();'

while(1) {
&ping_now;
$bad_list->update; # Update the widget now
sleep(60);
}

This code could also be called by a button to start the whole process. This could be used if you wanted the user to define the node names and then run "ping_now" on the user provided list.

 
That's a step in the right direction, but unfortunately spawns a new problem. A capture of network traffic shows that it does indeed ping minutely with your while loop in place, but unfortunately it stays in the loop pinging away and never gets around to drawing the gui. :-/
I tried the add a button method as well, and while it again pings minutely, it apparently blasts past the display update in &pingnow, and changes don't show up. Also, both methods render the gui immobile on the desktop (i.e. can't reposition where it sits), and requires an actual process kill from the task manager to exit. Suggestions?
 
This solution should work better. Perl/Tk includes a command to repeat commands every X milli-seconds. This command is attached to a widget. So to call ping_now every 60 sconds, do the following:

$bad_list->repeat(60000 => \&ping_now);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top