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.


In reply to Re: Coro::Channel, worker thread and timeout by Tanktalus
in thread Coro::Channel, worker thread and timeout by Marseille07

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.