in reply to Coro, EV and cede

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?

Replies are listed 'Best First'.
Re^2: Coro, EV and cede
by Nick Kostirya (Sexton) on Apr 07, 2011 at 12:01 UTC
    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.

        No, I've checked at once - it doesn't depend on LWP.

        The same is going on with any EV-based module. For example, with AnyEvent::HTTP or with just EV.

      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.

        CORE::select is used to slow down the output. Its change to Coro select is equal to the call of Coro sleep 0. Remove the select at all if you wish.

        The problem will still persist with using AnyEvent::HTTP.

Re^2: Coro, EV and cede
by Anonymous Monk on Dec 23, 2011 at 00:00 UTC
    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.