Skip to content

Instantly share code, notes, and snippets.

@ephphatha
Created September 5, 2018 08:23
Show Gist options
  • Save ephphatha/dcbb8ae0660fe92be67861eb5cb437d8 to your computer and use it in GitHub Desktop.
Save ephphatha/dcbb8ae0660fe92be67861eb5cb437d8 to your computer and use it in GitHub Desktop.
quick and dirty test script to work out how reads on non-blocking pipes behave
#! /usr/bin/perl
use strict;
use warnings;
use feature qw(say);
use IO::Select;
use IO::Pipe;
my $select = new IO::Select;
for my $c (1 .. 20) {
my $pipe = IO::Pipe->new();
if (my $childpid = fork()) {
say "Spawned child with pid $childpid";
$pipe->reader;
$pipe->blocking(0);
$select->add($pipe);
} else {
$pipe->writer;
$pipe->autoflush;
select $pipe;
my $timeout = int(rand(10));
say "[$$] Inside child";
say "[$$] sleeping for $timeout seconds until next line";
my $sleeptime = sleep($timeout);
say "[$$] Slept for $sleeptime seconds, now I'm done";
exit(0);
}
}
say "Finished spawning children";
while (my @ready_fh = $select->can_read) {
for my $hd (@ready_fh) {
if ($hd->eof) {
$select->remove($hd);
}
while (defined(my $_= $hd->getline)) {
print "Got line: $_";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment