Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day Lucas,

Welcome to the Monastery.

Firstly, you can put long tracts of code within <readmore>...</readmore> or <spoiler>...</spoiler> tags: see "Writeup Formatting Tips" for details.

Unfortunately, without seeing the surrounding code, it's difficult to tell what additional code will be appropriate. There may well be better solutions!

The following demonstrates how you might achieve what you ask using an alarm (actually, it uses Time::HiRes's ualarm for finer granularity).

#!/usr/bin/env perl -l use strict; use warnings; use Time::HiRes qw{ualarm time}; my $timeout = 1_000_000; #microseconds my $limit = 3; my @packets = (0 .. 11); print time; { my $local_limit = $limit; local $SIG{ALRM} = sub { print time; $local_limit = $limit; ualarm $timeout; }; ualarm $timeout; while (1) { next unless $local_limit-- > 0; last unless @packets; my $line = shift @packets; ualarm 1 if $line == 6; print $line; } }

Here's a sample run:

1474530606.74812 0 1 2 1474530607.75322 3 4 5 1474530608.75744 6 1474530608.75752 7 8 9 1474530609.75927 10 11

Notes:

  • I've set $limit to 3; you'll want 100 here.
  • I've used an array (@packets) instead of a file. Where I have:
    last unless @packets; my $line = shift @packets;
    You'll want something like:
    last if eof $fh; my $line = <$fh>;
    See eof.
  • The line
    ualarm 1 if $line == 6;
    is a bit of a kludge. It's intended to show what happens if you read less than $limit packets in the allotted time.

See also: "perlipc -- Perl interprocess communication".

Update: s/last unless eof/last if eof/

— Ken


In reply to Re: Limit Socket (throttle) to send specific ammount of packets every second by kcott
in thread Limit Socket (throttle) to send specific ammount of packets every second by Lucas Rey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found