Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Enlighten by Abigail-II's example. Tis a MCE::Hobo demonstration.

by marioroy (Prior)
on Mar 13, 2017 at 11:42 UTC ( [id://1184395]=CUFP: print w/replies, xml ) Need Help??

Hello,

I came across a cool post by Abigail-II and thought to try it with MCE::Hobo. Thank you, trippledubs for posting the link.

On Windows, MCE::Hobo spawns threads. Otherwise, childrens on Cygwin and other platforms. The following is a demonstration for many hobos, but never more than a fixed number at a given time.

use strict; use warnings; use MCE::Hobo; #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Based on http://www.perlmonks.org/?node_id=175715 ( by Abigail-II ). # Currently, MCE::Hobo emits a message to STDERR if unable to spawn. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub mhobo ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { MCE::Hobo->waitone() unless $c <= $max; exit(255) unless defined (my $h = MCE::Hobo->create($code, $c)); } MCE::Hobo->waitall(); } sub ahobo (\@$&) { my ($data, $max, $code) = @_; my $c = 0; foreach my $data (@$data) { MCE::Hobo->waitone() unless ++$c <= $max; exit(255) unless defined (my $h = MCE::Hobo->create($code, $data)) +; } MCE::Hobo->waitall(); } STDOUT->autoflush(1); # Perl 5.14 or higher mhobo 9, 3, sub { print $_[0]."\n"; for (1 .. 4e7) { 1 } # simulate busy }; my @input = ( 'a' .. 'i' ); ahobo @input, 3, sub { print $_[0]."\n"; for (1 .. 4e7) { 1 } # ditto };

Regards, Mario.

Replies are listed 'Best First'.
Re: Enlighten by Abigail-II's example. Tis a MCE::Hobo demonstration.
by trippledubs (Deacon) on Mar 14, 2017 at 04:22 UTC

    Hi Mario,

    I tried doing a simple test on Linux and Windows

    my $start = localtime; mfork(4,4, sub { my $x = 1e9; $x-- until $x == 1; print "$$\n"; }); my $end = localtime; my $t1 = $end - $start; print "Seconds: ",$t1->seconds,"\n";
    vs.
    my $start = localtime; mhobo(4,4, sub { my $x = 1e9; $x-- until $x == 1; print "$$\n"; }); my $end = localtime; my $t1 = $end - $start; print "Seconds: ",$t1->seconds,"\n";
    Is this a valid benchmark?

      A while back, MCE::Hobo spawned children on the Windows platform until recently, now threads. Hence, one might want the thread-id in the output. There is no "wait" equivalent for threads. Therefore, MCE::Hobo->waitone is not yet optimized on Windows. Other MCE::Hobo methods are fine.

      use strict; use warnings; use if $^O eq 'MSWin32', 'threads'; use Time::HiRes 'time'; use MCE::Hobo; my $tid = 0; sub CLONE { $tid = threads->tid() if $INC{'threads.pm'}; } sub mhobo ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { MCE::Hobo->waitone() unless $c <= $max; exit(255) unless defined (my $h = MCE::Hobo->create($code, $c) +); } MCE::Hobo->waitall(); } my $start = time; mhobo(12, 4, sub { my $x = 1e8; $x-- while $x; print "$$.$tid\n"; }); printf "Seconds: %0.03f\n", time - $start;

      Please know that MCE::Hobo was an exercise for a threads-like or forks-like companion to MCE::Shared.

      Regards, Mario.

        MCE::Shared 1.817 has been released. The reason is to have MCE::Hobo run consistenly on all platforms including Windows. From 1.817 onwards, MCE::Hobo will spawn a process via fork on Windows, similar to running on any Unix OS.

        use strict; use warnings; use MCE::Hobo; use Time::HiRes 'time'; sub mhobo ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { MCE::Hobo->waitone() unless $c <= $max; exit(255) unless defined (my $h = MCE::Hobo->create($code, $c) +); } MCE::Hobo->waitall(); } my $start = time; mhobo(12, 4, sub { my $x = 3e7; $x-- while $x; print "$$\n"; }); printf "Seconds: %0.03f\n", time - $start;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2024-04-25 01:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found