Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: coderef for 1 .. 2?

by ronstudio (Novice)
on Nov 02, 2017 at 03:17 UTC ( [id://1202569]=note: print w/replies, xml ) Need Help??


in reply to Re: coderef for 1 .. 2?
in thread coderef for 1 .. 2?

Thanks kcott and everyone for the time and energy involved with this. I feel obligated that I should go further to ask the module author for help if he can explain it further on the example. The mentioned 2 lines
my $end = $delay->begin; ... $end->();
are related to IOLoop. From the documentation, each $delay->begin would increment the event counter and the returned code reference can be used as a callback. The callback needs to be executed when the event has completed to decrement the event counter again. However, I just cannot understand the recursion in this case failed to continue calling itself without the for loop.

The following is the question that I raised in the issue report:
https://github.com/kraih/mojo/issues/1147#issuecomment-341304178

Replies are listed 'Best First'.
Re^3: coderef for 1 .. 2?
by ronstudio (Novice) on Nov 02, 2017 at 07:34 UTC
    Hi Ken,

    Many thanks for your help again.
    I think I have finally got it!

    The code example is basically the same example as this AnyEvent example from Perl Maven:
    https://perlmaven.com/fetching-several-web-pages-in-parallel-using-anyevent

    The "condvar" in AnyEvent is basically the same "delay" in Mojo. I find it easier to understand in the context of AnyEvent that condvar "condition variable" represent a condition that must become true.

    You'll notice the sequence and logic of the codes in both Perl Maven and Mojo cookbook are the same.
    I am wondering if it is possible for the event-loop to finish (and exit) before it can start calling the inner function.

    So as what you have found, the problem is related to the codes for loop control:
    ### my $end = $delay->begin; ... ### $end->();
    I notice if I change the sequence of the $end->(); as following:
    $fetch->(); $end->();
    Instead of
    $end->(); $fetch->();
    The code will now process all the URLs one by one when it is only $fetch->().
    And if I run it with for-loop like $fetch->() for 1 .. 2, it looks like processing 2 URLs in 1 go.
    The code behaving much more like the original intended.

    The problem with the original code is that if you are calling it 1 time only, at the time reaching the end of the event loop the condition may have satisfied and exit the program before it can call another function itself.(my guess)
    (I hope the above phase are correct in describing the issue. This event loop is too new for me)
      Thanks again for everyone's help.

      I have posted the question into the module mailing support:
      https://groups.google.com/forum/#!topic/mojolicious/HftNVDTfM74

      I think my theory is correct that the original code is having the issue that the loop has finished before it can call the inner function itself.

      Thanks everyone!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202569]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found