Hi, I'm having troble handling timeouts in the following code. I'm trying to implement Coro::Channel with worker threads where each thread simply does tcp_connect on bunch of hosts. However, I'm having trouble going to the next iteration of the worker loop from the AnyEvent->timer call back. I tried next (which doesn't work within the loop), next with label on the loop (didn't work because the Event Loop didn't recognize the label) so forth. I'm sure there is a way to do this that I'm not aware, otherwise it makes it very difficult to implement timeouts from a worker-thread using AnyEvent. Any thoughts or insights are greatly appreciated. Thanks.
#!/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"; retu +rn; } # 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( $_ ); }

In reply to 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.