The following is a demonstration spawning 2 child processes. Yield is an exclusive action causing other worker(s) to wait till the next interval period, as seen in the output.

Demonstration 2

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

Output

$ perl demo2.pl Parent 1 Inside child #1 1646276375.98194 Inside child #2 1646276376.18194 Inside child #1 1646276376.38187 Inside child #2 1646276376.58187 Inside child #1 1646276376.78162 Parent 2 Inside child #2 1646276376.98187 Inside child #1 1646276377.18188 Inside child #2 1646276377.38187 Inside child #1 1646276377.58188 Inside child #2 1646276377.78186 Parent 3 Inside child #1 1646276377.98187 Inside child #2 1646276378.18187 Inside child #1 1646276378.38187 Inside child #2 1646276378.58187 Inside child #1 1646276378.78187 Parent 4 Inside child #2 1646276378.98187 Inside child #1 1646276379.18188 Inside child #2 1646276379.38187 Inside child #1 1646276379.58187 Inside child #2 1646276379.78187 Parent 5 Inside child #1 1646276379.98187 Inside child #2 1646276380.18187 Inside child #1 1646276380.38188 Inside child #2 1646276380.58187 Inside child #1 1646276380.78187

In reply to Re^2: 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.