After implementing coroutines on the C level as well (ugly but necessary),
Coro-0.11 finally seems to work in all corner cases I tested and also
includes two modules that make non-blocking I/O easier (that require the
bugfix I posted earlier ;).
Here is a parallelized finger client (except for name resolution it is
non-blocking with respect to other coroutines):
use Coro;
use Coro::Event;
use Coro::Handle;
use Coro::Socket;
sub finger {
my $user = shift;
my $host = shift;
my $fh = new_inet Coro::Socket PeerHost => $host, PeerPort => "finger" or die;
print $fh "$user\n";
print "$user\@$host: $_" while <$fh>;
print "$user\@$host: done\n";
}
async { finger "trouble", "noc.dfn.de" };
async { finger "root", "localhost" };
loop;
And here is a command interpreter that queues finger requests in the background:
sub keyboard : Coro {
my $stdin = new_from_fh Coro::Handle \*STDIN;
$|=1;
while() {
print "cmd> "; my $cmd = <$stdin>; chomp $cmd;
if ($cmd eq "finger") {
print "user> "; my $user = <$stdin>; chomp $user;
print "host> "; my $host = <$stdin>; chomp $host;
async { finger($user, $host) };
} elsif ($cmd eq "quit") {
unloop;
terminate;
} else {
print "unknown command '$cmd', either 'finger' or 'quit'\n";
}
}
}
loop;
And here is (off-topic), how continuations work now (after Damian Conway
convinced me to use yield ;):
# a stateful random number generator
sub badrand : Cont {
my $seed = 1;
while() {
$seed = $seed * 121 % 97;
yield $seed % $_[0];
}
}
print badrand($_), " " for 1..30; print "\n";
To date, Coro was only tested under Linux/x86 + glibc-2.2.2 && 2.2.3
and perl-5.6.1 and 5.7.2. Threads do not work (under linux, because
of bugs in linuxthreads, YMMV).
I'd be very much interested in other platforms (e.g. windows, other unix
flavours) because multithreading on the c level is not that portable.
The overhead also doesn't seem to be too large (a standard perl mutator
object method isn't much faster). If Event would implement the callback
that Coro::Event uses to schedle in XS I don't think it would be noticably
slower than the standard callback way.
--
-----==- |
----==-- _ |
---==---(_)__ __ ____ __ Marc Lehmann +--
--==---/ / _ \/ // /\ \/ / [EMAIL PROTECTED] |e|
-=====/_/_//_/\_,_/ /_/\_\ XX11-RIPE --+
The choice of a GNU generation |
|