Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (2)
    As of 2018-05-21 01:34 GMT
    Find Nodes?
      Voting Booth?