Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Run subroutine occasionally

by marioroy (Prior)
on Mar 03, 2022 at 03:19 UTC ( [id://11141769]=note: print w/replies, xml ) Need Help??


in reply to Re: Run subroutine occasionally
in thread Run subroutine occasionally

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141769]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found