The yield function in MCE::Child (ditto for MCE::Hobo) retains interval periods; i.e. do something at/near every N fractional seconds. It works quite well, even among many workers.

use strict; use warnings; use MCE::Child; use Time::HiRes qw(sleep time); STDOUT->autoflush(1); # do something periodically in the background my $child = MCE::Child->create(sub { while (1) { MCE::Child->yield(300.0); print "Inside child ", time, $/; } }); # application code print "Parent", $/; # reap child $child->kill; $child->join;

Demonstration

Notice the output for the next demonstration where the child displays output every 0.2 seconds, even simulating work. It does a delta behind the scene from the last yield statement in order to retain near/exact intervals.

use strict; use warnings; use MCE::Child; use Time::HiRes qw(sleep time); STDOUT->autoflush(1); my $child = MCE::Child->create(sub { while (1) { MCE::Child->yield(0.200); print "Inside child ", time, $/; sleep 0.1; # simulate work } }); for (1..5) { print "Parent ", $_, $/; sleep 1; } $child->kill; $child->join;

Output

$ perl demo.pl Parent 1 Inside child 1646249559.98288 Inside child 1646249560.18285 Inside child 1646249560.38241 Inside child 1646249560.58239 Parent 2 Inside child 1646249560.78239 Inside child 1646249560.98283 Inside child 1646249561.18284 Inside child 1646249561.38284 Inside child 1646249561.58284 Inside child 1646249561.7825 Parent 3 Inside child 1646249561.98284 Inside child 1646249562.18284 Inside child 1646249562.38284 Inside child 1646249562.58284 Inside child 1646249562.78284 Parent 4 Inside child 1646249562.98284 Inside child 1646249563.18284 Inside child 1646249563.38284 Inside child 1646249563.58284 Inside child 1646249563.78283 Parent 5 Inside child 1646249563.98284 Inside child 1646249564.18284 Inside child 1646249564.38284 Inside child 1646249564.58284 Inside child 1646249564.78284

In reply to Re: Run subroutine occasionally by marioroy
in thread Run subroutine occasionally by rementis

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.