Thanks for the clarifications - that helps a lot. Having played around with this now I think that the problem you are having essentially boils down to the fact that the event loop is blocking because you are telling it to and that there's no reason for you to do so.

Firstly, in order to simplify things I have decided to concentrate solely on AnyEvent (because that's what I'm less unfamiliar with) and ditch IO::Async for this illustration. That gets rid of the need for the glue and stops the chances of 2 different event loops coming to blows. As a starting point, here are two timers running on 5 and 7 second intervals implemented purely with AnyEvent. There's a third timer there just so the thing doesn't carry on forever.

#!/usr/bin/env perl use strict; use warnings; use AnyEvent; my $cv = AnyEvent->condvar; my @timers = ( AnyEvent->timer ( after => 2, interval => 5, cb => sub { warn "Timer 1 at " . AnyEvent->now . "\n"; } ), AnyEvent->timer ( after => 2, interval => 7, cb => sub { warn "Timer 2 at " . AnyEvent->now . "\n"; } ), AnyEvent->timer ( after => 20, cb => sub { warn "Timer 3 at " . AnyEvent->now . "\n"; $cv->send; } ), ); $cv->recv;

If you run this you will see something along these lines:

Timer 1 at 1591953440.37264 Timer 2 at 1591953440.37264 Timer 1 at 1591953445.37213 Timer 2 at 1591953447.37533 Timer 1 at 1591953450.36854 Timer 2 at 1591953454.38475 Timer 1 at 1591953455.36656 Timer 3 at 1591953458.37977

So the timers seem to be working on their correct schedules. Now we can add in some pings. I've reduced the numbers here so we don't get bucketloads of output, but the principle should be the same.

#!/usr/bin/env perl use strict; use warnings; use AnyEvent; use AnyEvent::Ping; use Data::Dumper; my @todoList = map { "192.168.100." . $_ } (1 .. 5); my $cv = AnyEvent->condvar; my @timers = ( AnyEvent->timer ( after => 2, interval => 5, cb => sub { warn "Timer 1 at " . AnyEvent->now . "\n"; ping_hosts (1, @todoList); } ), AnyEvent->timer ( after => 2, interval => 7, cb => sub { warn "Timer 2 at " . AnyEvent->now . "\n"; ping_hosts (2, @todoList); } ), AnyEvent->timer ( after => 20, cb => sub { warn "Timer 3 at " . AnyEvent->now . "\n"; $cv->send; } ), ); $cv->recv; sub ping_hosts { my ($num, @hosts) = @_; my $ping_timeout = 2; #seconds to wait for response my $ping_attempts = 4; #number of pings to issue, my $ping_pktsize = 32; #packet size of ping my $ping = AnyEvent::Ping->new (packet_size => $ping_pktsize); $ping->timeout ($ping_timeout); #$cv_ping->begin(sub { shift->send($_[0]) }); foreach my $host (@hosts) { $ping->ping ( $host, $ping_attempts, sub { print "Pinger $num: $host state:" . Dumper ($_[0]) . " +\n"; } ); } }

I've simplified your ping loop/sub a bit and you'll notice that there is now no counter and importantly no cond_var. This is because you have no need to block here if all you are doing is firing off the pings. Now when we run this it runs to completion with the pings sent on the intended schedule.

This is my first go at using event loops so I am slowly grasping the concepts and how to implement it, but i'm struggling a little, so I'm not even sure if i'm doing this properly.

The concepts are tough - there's no doubt about that. I don't do much functional programming and even less on event loops so it was interesting to approach this from an equally inexpert standpoint. My simplistic approach with these is just not to block unless it's really necessary. That's not always easy to do when you are coming from a procedural background but it has helped me through when tackling this sort of task.

I hope this is useful for you and wish you luck with the rest of your project.


In reply to Re^3: Implementing AnyEvent::Ping multiple times by hippo
in thread Implementing AnyEvent::Ping multiple times by mmoorreett

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.