in reply to Coro::Channel, worker thread and timeout

Rough thoughts here. First off, the code you have will go and create 10 worker threads. I'm not sure why you need to do that. If you're just re-using threads because their overhead is too much, try async_pool. In fact, that's about what I'd do.

Second, inside your async_pool thread, you capture $Coro::current, and use that in a closure for your timer to wake it up or terminate it. Terminating it is fine - Coro will create a new thread for the pool later if required. Unfortunately, I'm not really sure where that timer is going, since you don't even have a comment explaining what goes on after that.

So, something like this:

async { while (my $host = $input->get()) # note that putting false will caus +e this to exit - probably a good thing { print "got $host\n"; async_pool { tcp_connect $host, 65432, Coro::rouse_cb; my ($fh) = Coro::rouse_wait; return unless $fh; print "got socket for $host\n"; my $coro = $Coro::current; my $t = AnyEvent->timer( after => 10, cb => sub { print "$host: +timed out\n"; $coro->cancel } ); # do stuff? } # async pool } }
Or something like that. Of course, this can get far more threads going at once, but is still likely to be handled easily, unless perhaps you go up to hundreds or maybe thousands of hosts that you're dealing with, and even then, it's more a network issue than a Coro issue.

Replies are listed 'Best First'.
Re^2: Coro::Channel, worker thread and timeout
by Marseille07 (Acolyte) on Sep 14, 2011 at 05:28 UTC
    I think your suggestion is more elegant than what I had originally with the 10 worker threads. And $Coro::current->cancel appears like a sure way to get rid of a coro on timeout, so I'll try quick samples and see what happens. Thank you for sharing a thorough example.