MCE::Util(3)

๐Ÿ’– MCE::Util โ€” Utility functions ๐Ÿ’–

okay bestie, this is the MCE::Util page from the User Contributed Perl Documentation, and it's literally just the lil' helper drawer for MCE. let's get into it ๐Ÿซถ


โœจ VERSION โœจ

This document describes MCE::Util version 1.902. yes, that's the version. no, don't ask me to make it cuter, it's a number ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

use MCE::Util;

that's it. that's the whole vibe. so simple. so clean. icon behavior honestly โœจ


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so this is a utility module for MCE โ€” like the little purse of tools that MCE carries around. and here's the tea: nothing is exported by default. nope. not a single thing. you gotta ask for it.

the one thing you can export is get_ncpu. that's the only exportable, periodt.

๐Ÿ’… get_ncpu() ๐Ÿ’…

okay so this one returns the number of logical (online/active/enabled) CPU cores. and โ€” important โ€” it will never be smaller than one. so even if your machine is being weird about it, you get a 1 minimum. no zeros, no negative nonsense, we don't do that here ๐Ÿ™ˆ

my $ncpu = MCE::Util::get_ncpu();

now, the fun part: if you specify 'auto' for max_workers, it calls MCE::Util::get_ncpu automatically for you. but listen โ€” MCE 1.521 sets an upper-limit when specifying 'auto'. why? mainly to safeguard apps from spawning 100 workers on a box having 100 cores. that's a lot of workers, bestie. and it's especially important for apps that are IO-bound. iykyk.

here's the whole thing in code, exactly as it lives:

use MCE;

## 'Auto' is the total # of logical cores (lcores) (8 maximum, MCE 1.521).
## The computed value will not exceed the # of logical cores on the box.

my $mce = MCE->new(

max_workers => 'auto',       ##  1 on HW with 1-lcores;  2 on  2-lcores
max_workers =>  16,          ## 16 on HW with 4-lcores; 16 on 32-lcores

max_workers => 'auto',       ##  4 on HW with 4-lcores;  8 on 16-lcores
max_workers => 'auto*1.5',   ##  4 on HW with 4-lcores; 12 on 16-lcores
max_workers => 'auto*2.0',   ##  4 on HW with 4-lcores; 16 on 16-lcores
max_workers => 'auto/2.0',   ##  2 on HW with 4-lcores;  4 on 16-lcores
max_workers => 'auto+3',     ##  4 on HW with 4-lcores; 11 on 16-lcores
max_workers => 'auto-1',     ##  3 on HW with 4-lcores;  7 on 16-lcores

max_workers => MCE::Util::get_ncpu,   ## run on all lcores
);

the way that 'auto' can do math now?? *, /, +, - โ€” it's giving calculator energy ๐Ÿงฎโœจ

โญ In summary โญ

  1. Auto has an upper-limit of 8 in MCE 1.521 (# of lcores, 8 maximum)
  2. Math may be applied with auto (*/+-) to change the upper limit
  3. The computed value for auto will not exceed the total # of lcores
  4. One can specify max_workers explicitly to a hard value
  5. MCE::Util::get_ncpu returns the actual # of lcores

like, five whole facts. we ate. ๐Ÿ“


๐ŸŽ€ ACKNOWLEDGMENTS ๐ŸŽ€

shoutout time!! the portable code for detecting the number of processors was adopted from Test::Smoke::SysInfo. giving credit where credit is due, as one does ๐Ÿ’œ


๐ŸŒˆ INDEX ๐ŸŒˆ

  • MCE
  • MCE::Core

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Mario E. Roy, <marioeroy AT gmail DOT com>


and that's the whole page bestie!! this was written for perl v5.42.0 on 2025-09-10, and it's MCE::Util(3) from the User Contributed Perl Documentation. go forth and count your cores, you absolute icon ๐Ÿ’–โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from