Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Re: network socket send loop is very cpu intensive

by Anonymous Monk
on Nov 18, 2003 at 13:42 UTC ( #307965=note: print w/replies, xml ) Need Help??

in reply to Re: network socket send loop is very cpu intensive
in thread network socket send loop is very cpu intensive

thanks, all useful. I originally did have the close handler inside the main loop but changed it to see if that helped things. I have also experimented with sleeping for a second before reading again and does sort it out. It has helped me to have you talk me through it though. With regards Update 2, just a typo from a version change. Update 1, I'll check into that, thanks.
  • Comment on Re: Re: network socket send loop is very cpu intensive

Replies are listed 'Best First'.
Re: Re: Re: network socket send loop is very cpu intensive
by AcidHawk (Vicar) on Nov 18, 2003 at 14:21 UTC

    No problem, I like to have one exit point also, so I put this little bit together to illustrate the point...

    This is UNTESTED though..

    #! /usr/bin/perl use strict; use warnings; my $rc = 0; while (1) { if (opendir DH, "/tmp/store") { if (readdir(DH) ) { next if $_ eq "." or $_ eq ".."; my @data = split /_/; my $mobile = $data[1]; undef @data; chomp $mobile; s/^\s+//, s/\s+$//, s/\s\s+/ / for $mobile; if (open FH,"/tmp/store/$_") { undef $/; my $data = <FH>; chomp $data; my @fields = split; my $appcode = $fields[0]; my $message = $data; #Assuming you want to clean up the data that is being +sent to sendtolive() s/^\s+//, s/\s+$//, s/\s\s+/ / for $message; close FH; # Sends data through socket connection assuming the se +ndtolive returns a success or fail # you can test for it and only delete if it is success +ful. Do you want to delete the file if it cant send the sms..? if (sendtolive($mobile,$message)) { # remove file once read unlink("/tmp/store/$_"); } else { print "Count NOT sent $message: sendtolive failed" +; } } else { print "could not open: $_ $!"; $rc = 3; last; } } else { print "Cannot read dir /tmp/store: $!\n"; $rc = 2; last; } #You have finished with this file loop onto the next. closedir (DH); sleep(1); } else { print "Faile to open dir /tmp/store: $!\n"; $rc = 1; last; } } exit ($rc);

    Of all the things I've lost in my life, its my mind I miss the most.
      looks good to me, thanks a lot. what are the conventions for returning values from subroutines in Perl? For example if I want to indicate all is not well should I return 1 or 0 or false or undef ?? Jon

        Well, I'm not sure about the conventions but I have gotten used to returning 1 for success or 0 for fail. That way I can easily do something like..

        if(&mysubroutine()) { ... #continue as mysubroutine was successful } else { ... Something in mysubroutine failed } sub mysubroutine() { my $rc = 0; #this is returned if $rc is not modified which indicates a failure +... #as a silly example we will open a file if (open FH, "<./test.txt") { ... #read in file as the open was successful $rc = 1; } return($rc); }

        Of all the things I've lost in my life, its my mind I miss the most.

        Best bet would be to stick with the same conventions Perl uses, this way you can maintain a perlish feel to your code making use of 'and' and 'or' so on and so forth. So returning 0, an empty string, or undef are all valid canidates.

        Also consider using negative error codes for non fatal errors. I.e all data was present, but a field had some invalid element. This may not be a reason to 'die', but to send it through an extended scrubbing routine.

        Just a comment on style, try to segregate lines which do functionally different things. This way it is clear when reading the code that lines grouped together are doing something similar. I have included your code below and spaced it out along what I mean. Also try to not "hardcode" values in your scripts, as this can lead to numerous possibilities for typos. This is just my personal opinion and YMMV, and offered as constructive criticism.

        #!/usr/bin/perl my $dir = "/tmp/store"; my $sleep = '1'; opendir DH, $dir or die "Opendir $dir: $!\n"; while (1) { for ( grep !/^\.+$/, readdir(DH) ) { my $file = join('/', $dir, $_); # split $_ on _ and return the second element # this avoids a unnecessary temp array my $mobile = ( split /_/ )[1]; # Is the extra whitespace cleanup really necessary? $mobile =~ s/(?:^\s+|\s+$)//g; # drop spaces from front and back $mobile =~ s/\s+/ /g; # sanitize the rest my $old_sep = $/; # being paranoid, save it for later undef $/; open FH, $file or die "open $file: $!\n"; chomp( my $data = <FH> ); close FH; $/ = $old_sep; # restore it, in case other funcs expect the defa +ult # im not sure what the next few lines are doing, as they are # working on $data or $_, almost randomly. my @fields = split; my $appcode = $fields[0]; # this is never used again? my $message = $data; s/^\s+//, s/\s+$//, s/\s\s+/ / for $data; # should this be $mess +age? sendtolive($mobile, $message); # Sends data through socket conne +ction unlink("$file"); # remove file once read } # END for ( grep readdir(DH) ) closedir(DH); sleep $sleep; } # END while 1

        use perl;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://307965]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2021-04-21 08:13 GMT
Find Nodes?
    Voting Booth?

    No recent polls found