Consider the code below, where CPU crunching is done when Event loop is idle, but only for 0.1s to allow HTTP requests about the processed data to not be delayed more than ~100mS.

Is there any better way to do this that would eliminate the (up-to) 100mS wait time for a request to actioned? Basically... is there any way idle could run continously and be interuptted by the events? What is generally used in this situation. A second thread?

Thanks!

use strict; use warnings; use AnyEvent; use AnyEvent::HTTPD; use Time::HiRes qw(gettimeofday); use Digest::MD5 qw(md5); my $httpd = AnyEvent::HTTPD->new(port=>9090); my $count = 0; my $foo = 'bar'; # Handle other events $httpd->reg_cb( '/count' => sub { my ($h,$r)= @_; $r->respond({content=>['text/plain',"$count: ".(unpack 'q',$fo +o)]}); $h->stop_request; }); # CPU crunching for 100mS when idle... my $idle = AnyEvent->idle(cb => sub { my $now = gettimeofday; while(gettimeofday < ($now + 0.1)) { $foo = md5($foo); } $count+=1; }); my $cv = AnyEvent->condvar; $cv->recv;

In reply to 'background' CPU process when Idle in AnyEvent by sectokia

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.