#!/usr/local/bin/perl use strict; use warnings; use AnyEvent; use AnyEvent::Socket; use Coro; use Coro::AnyEvent; my $input = new Coro::Channel 5; foreach my $i ( 1 .. 10 ) { async { while (1) { my $host = $input->get(); print "$i got $host\n"; tcp_connect $host, 65432, Coro::rouse_cb; my ($fh) = Coro::rouse_wait; next unless $fh; print "$i got socket for $host\n"; my $t; $t = AnyEvent->timer ( after => 10, cb => sub { undef $t; print "$host: timed out\n"; return; } # ideally I want to move onto the next iteration of the parent loop ); } }; } my @hosts = qw( host1.server.com host2.server.com ); foreach ( @hosts ) { $input->put( $_ ); }