Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:51:01+00:00 2026-05-24T03:51:01+00:00

I start to learn AnyEvent and have some trobles with it. I totally misunderstood

  • 0

I start to learn AnyEvent and have some trobles with it.
I totally misunderstood how its possible to get asynchronous profit, fe :

#!/usr/bin/env perl

package LatencySub;

use strict;
use warnings;
use AnyEvent;

# sub for emulate latency - is it right way?
sub do_delay{
    my ($name, $delay) = (@_);
    my $cv = AE::cv;
    my $timer = AE::timer $delay, 0, sub { $cv->send() };
    $cv->recv;
    return $name.' proceed, delay is '.$delay;
};


package main;

use 5.12.0;
use warnings;

use Smart::Comments;

use AnyEvent;

my @list = (
    { name => 'first', delay => 1  },
    { name => 'second', delay => 1 },
    { name => 'third', delay => 2 }
);

sub process_cb {
    my ( $name, $delay, $cb ) = @_;
    my $result = LatencySub::do_delay( $name, $delay );
    $cb->($result);
}

my %result;

my $cv = AE::cv;
# outer loop
$cv->begin (sub { shift->send (\%result) });

my $before_time =  AE::time;
### foreach start...
foreach my $entity (@list) {
            $cv->begin;
            process_cb ( 
                                $entity->{'name'},
                                $entity->{'delay'},
                                sub {
                                     $result{$entity->{'name'}} = shift;
                                     $cv->end;
                                }
            );
     }
### foreach end...

$cv->end;
my $time_all = AE::time - $before_time;

### $time_all
### %result

At output I got:

### foreach start...

### foreach end...

### $time_all: '4.02105116844177'
### %result: {
###            first => 'first proceed, delay is 1',
###            second => 'second proceed, delay is 1',
###            third => 'third proceed, delay is 2'
###          }

All delay sum (1+1+2) eq $time_all – 4 seconds.
So, no profit at all.

Why is it and how I can (and is it possible?) create “right” callback?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:51:01+00:00Added an answer on May 24, 2026 at 3:51 am

    Don’t use condvars except to block your top-level program while waiting for events to complete. Using condvars makes it very difficult to reuse code; any function that has a condvar internally can never be safely used in a program that has another function that has a condvar in it. (This is not true if you never call recv and only use cb. But still… it’s dangerous and not for those that don’t know what they’re doing.)

    My rule: if the filename ends .pm, no condvars!

    If you want to run multiple things in parallel and run some more code once all the results are available, try Event::Join:

    sub delay($$) {
        AnyEvent->timer( after => $_[0], cb => $_[1] );
    }
    
    my $join = Event::Join->new(
        on_completion => sub { say "Everything is done" }
        events        => [qw/t1 t2 t3/],
    );
    
    delay 1, $join->event_sender_for('t1');
    delay 2, $join->event_sender_for('t2');
    delay 3, $join->event_sender_for('t3');
    

    Then, after 3 seconds, you’ll see “everything is done”. Event::Join is like begin and end on condvars, but can never block your program. So it’s easy to reuse code that uses it. Also, events are named, so you can collect results as a hash instead of just calling a callback when other callbacks are called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which family should I start to learn? (Never did any programming on microcontroller)
As I learn more and more about OOP, and start to implement various design
I start by creating a string variable with some non-ascii utf-8 encoded data on
I to start learn wxPython is usin YouTube, I watched 14 tutorials, and could
I start to learn Standard ML, and now I try to use Standard ML
I just start to learn web programming using IIS 7.5 in windows 2008 R2,
After programming for a while in C, I decided to finally start to learn
Dear all. Today, I start to learn iOS. I don't know what tool I
Start a new Silverlight application... and in the code behind (in the Loaded event),
I start to write a client - server application using .net (C#) for both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.