Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Coro, EV and cede

by Nick Kostirya (Sexton)
on Apr 07, 2011 at 08:18 UTC ( [id://897972]=perlquestion: print w/replies, xml ) Need Help??

Nick Kostirya has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to try the Coro together with EV.

There are a few code snippets:

async { while (1) { ... cede; } };

When I use only Coro, everything's fine.

But when I try to connect EV, weird things happen, which can be explained only by the fact cede doesn't pass the control to EV.

It forces me to use cede and Coro::Timer::sleep together:

async { while (1) { ... sleep 0; cede; } };

Coro::Timer::sleep - it's an explicit call to EV.

Has anyone faced such an issue?


UPDATE:
I was wrong, Coro::AnyEvent::poll really helps.
This is the same as sleep(0).
I was pulling his leg, Coro’s author, shame on me.
I just could not figure it out that Coro can be used without IO in practice And those using IO have Coro::AnyEvent::poll.
Still, I learnt that Marc Lehmann is a patient and very polite person.
I got a good lesson too: don’t make the output on STDIN artificially.

Replies are listed 'Best First'.
Re: Coro, EV and cede
by ikegami (Patriarch) on Apr 07, 2011 at 15:58 UTC
    The docs suggest
    async { EV::loop };

    That makes me wonder. If that works, then why doesn't cede to the main thread?

      I've tried this way at once, but it didn't help.

      Probably cede really doesn't give EV control.

        Just call sleep instead of cede if it works better.

        But you may want to consider the output I got from different sleep lengths in the second thread. I changed from printing the length to print time().

        Using sleep(0), it takes 9s to do a request. first second second second second second second second second second second second second second second second second 1302281756 second first second second second second second second second second second second second second second second second second second 1302281765 Using sleep(1), it takes 2s to do a request. first second second second 1302281791 first second second 1302281793 first second second 1302281795 Using sleep(2), it takes 1s to do a request. first second 1302281819 first second 1302281820 first second 1302281822 first 1302281823

        It gives control to the coro running EventLoop. But of course, that coro is in the EventLoop call. What about

        async { sleep 60 while 1; }
Re: Coro, EV and cede
by zwon (Abbot) on Apr 07, 2011 at 10:53 UTC

    I don't really understand what you're trying to do. Do you have some complete (but not too big) peace of code that we could run to see the problem?

      use strict; use warnings; use Coro; use Coro::EV; use Coro::LWP; use LWP::Simple; use Coro::Timer qw(sleep); async { while (1) { CORE::select undef, undef, undef, 0.5; print "first\n"; # my $r = get "http://www.perlmonks.org"; # print length $r, "\n"; # sleep 0; cede; } }; async { while (1) { CORE::select undef, undef, undef, 0.5; print "second\n"; # sleep 0; cede; } }; EV::loop while 1;
      output:
      first second first second first second ...
      When uncomment
      my $r = get "http://www.perlmonks.org"; print length $r, "\n";
      got output:
      first second second second second second second second
      Then uncomment "sleep 0" and got output :
      first second 76573 second first second 76570 second first second 76597 second first second

        Depending on your version of LWP, LWP::Simple might or might not play nice with LWP::Coro. I know that at least for some versions prior to 5.827, LWP::Simple used its own socket factory instead of using what LWP::UserAgent did.

        You might want to inspect your module versions.

        Be careful when upgrading to LWP 6.x. If you use HTTPS connections, you will at least also need to install Mozilla::UA, because LWP 6 checks the validity of https certificates.

        The first result shows that cede works, as you get your switching. The problem starts after LWP comes in, and looking into Coro::LWP documentation, it looks like it does something rather cumbersome. I don't see it even tries to establish connection with the server. But if I replace CORE::select with just select everything is ok.

        I'd say you should try something else. I had positive experience with AnyEvent::HTTP, or maybe threads.

      Sorry to reply on your post, I wish perlmonks had a way to anonymously + reply to postings, but I haven't found it yet. Anyway, Coro::AnyEvent::poll *is* the right function, and it does work + fine on my box. The output is this: first second second second second second second second second second second second second second second second second second second second second 73246 second first second second second second second The problem the original poster likely runs into is his use of CORE::s +elect - this function blocks the process for half a second each time +it's called, so LWP needs long time to make progress, because it can +only do something twice a second.
        I was wrong, Coro::AnyEvent::poll really helps. This is the same as sleep(0). I was pulling his leg, Coro’s author, shame on me. I just could not figure it out that Coro can be used without IO in practice And those using IO have Coro::AnyEvent::poll. Still, I learnt that Marc Lehmann is a patient and very polite person. I got a good lesson too: don’t make the output on STDIN artificially.
Re: Coro, EV and cede
by Anonymous Monk on Jun 24, 2011 at 19:44 UTC
    The Coro::AnyEvent module has a few numbers to implement common forms of "waiting for something", for example Coro::AnyEvent::poll looks as if it were the function you want. Coro only makes thread switches when it must, and will only poll for events when it must - as long as you have a thread to run it will run that one.
      Coro::AnyEvent::poll does not help.
        I was wrong, Coro::AnyEvent::poll really helps. This is the same as sleep(0).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://897972]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found