Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
package Fork::Queue; use IO::Socket; sub new { my($this) = @_; my $class = ref($this) || $this; my $self = {}; bless $self, $class; $self->mksockpair(); return $self; } # make the socketpair sub mksockpair { my($self)=@_; my $creator=IO::Socket->new() or die(); my($reader,$writer); ($reader,$writer)=$creator->socketpair(AF_UNIX,SOCK_STREAM,PF_UNSP +EC); shutdown($reader,1); shutdown($writer,0); $self->{'READER'}=$reader; $self->{'WRITER'}=$writer; } # method to put something on the queue sub enqueue { my($self,@data)=@_; my($header,$buffer,$tosend); my($handle)=$self->{'WRITER'}; foreach my $item (@data) { $header=pack("N",length($item)); $buffer=$header . $item; $tosend=length($buffer); print $handle $buffer; $handle->flush; } } # # method to pull something off the queue # sub dequeue { my($self)=@_; my($header,$data); my($toread)=4; my($bytes_read)=0; my($handle)=$self->{'READER'}; # read 4 byte header while ($bytes_read < $toread) { $bytes_read+=read($handle,$header,$toread,$bytes_read); } $toread=unpack("N",$header); $bytes_read=0; # read the actual data while ($bytes_read < $toread) { $bytes_read+=read($handle,$data,$toread,$bytes_read); } return $data; } 1; # # my $q=Fork::Queue->new(); if (my $pid=fork()) { # parent, enqueue a list a scalars $q->enqueue("some","random","list","of","stuff","die"); } else { # child, pull items off the queue, exit when we get "die" # (otherwise dequeue blocks and the child never exits) my($item); while ($item=$q->dequeue()) { if ($item eq "die") { exit; } print "$item\n"; } }

In reply to Thread::Queue for non-threaded apps by kschwab

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2014-07-28 23:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (210 votes), past polls