-
Notifications
You must be signed in to change notification settings - Fork 1
/
io-nonblock-method.pl
executable file
·61 lines (50 loc) · 1.36 KB
/
io-nonblock-method.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env perl
use 5.10.1;
use warnings;
use strict;
use IO::Handle;
use AnyEvent;
use AnyEvent::Strict;
my $fh;
my $loops = 0;
my $name;
$| = 1;
STDOUT->print("Enter you name> ");
{ package perl::net;
sub GetName() {
my $input = $fh->getline();
$name = $input if( $input =~ /\w+/ );
unless( $name ) {
STDOUT->print("Enter you name> ");
}
}
sub MakeCounter {
my $condvar = AnyEvent->condvar;
return {
'cv' => $condvar,
'ticker' => AnyEvent->timer(
after => 0.033, # Non-blocking sleep 30 times per second
cb => sub {
$condvar->send;
}
)
}
}
1;
}
my $aeCounter = perl::net::MakeCounter;
my $aeWatcher = AnyEvent->io(
fh => ($fh = \*STDIN),
poll => "r",
cb => sub {
perl::net::GetName; # Blocking call nested in async callback
$aeCounter->{cv}->send; # Interrupts $aeCounter timeout
}
);
while(! $name ) { # Main Event Loop
$loops++;
$aeCounter->{cv}->recv; # Half-blocking: Waits for timeout BUT...
$aeCounter = perl::net::MakeCounter; # Will ALSO read other events like I\O via $aeWatcher
}
STDOUT->print("After $loops event loops your name is $name \n") if( $name );
1;