Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: network socket send loop is very cpu intensive

by AcidHawk (Vicar)
on Nov 18, 2003 at 12:48 UTC ( #307960=note: print w/replies, xml ) Need Help??


in reply to network socket send loop is very cpu intensive

Two things I've noticed.

  • You will never get to closedir DH; as you are in a while(1){} loop before.
  • You continuously loop in /tmp/store for files never sleeping or giving anything else a chance to run. So even if there are no files you read and read and read... etc you get the picture.. Try adding a sleep($timeout) under the unlink("/tmp/sms/$_"); # remove file once read where $timeout can be 1 sec
  • If you post the filename and data you are looking for, (to split) we'll have a better idea of what you are doing with the splits etc..

    Update1: Something else... you assign $message the value of $data. Then you clean up the whitespace from $data but then send $message to sendtolive()..?

    Update2: You open the /tmp/store dir and try to read a file int the /tmp/sms dir..?

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

    Replies are listed 'Best First'.
    Re: Re: network socket send loop is very cpu intensive
    by Anonymous Monk on Nov 18, 2003 at 13:42 UTC
      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.

        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

    Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2021-02-28 20:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?