Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Parallel trigger of different subs

by Anonymous Monk
on Jan 02, 2018 at 06:23 UTC ( #1206525=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to run 2 sub's parallel in perl?

Replies are listed 'Best First'.
Re: Parallel trigger of different subs
by karlgoethebier (Monsignor) on Jan 02, 2018 at 11:51 UTC
    "...2 sub's parallel in perl"

    Consider something like this totally useless little sketch:

    #!/usr/bin/env perl use strict; use warnings; use threads; use MCE::Hobo; use Data::Dump; use feature qw(say); use constant AMOUNT => 0; my $shared = MCE::Shared->hash(); my $nose = MCE::Hobo->new( \&nose, "cuke" ); my $foo = MCE::Hobo->new( \&foo, "bar" ); # $nose->join; # $foo->join; MCE::Hobo->wait_all(); my $hash_ref = $shared->export; dd $hash_ref; say $shared->{nose}; say $shared->{foo}; sub nose { sleep AMOUNT; $shared->set( nose => shift ); } sub foo { sleep AMOUNT; $shared->set( foo => shift ); } __END__

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Parallel trigger of different subs
by jahero (Monk) on Jan 02, 2018 at 08:04 UTC
Re: Parallel trigger of different subs
by BrowserUk (Pope) on Jan 02, 2018 at 12:22 UTC

    Simples:

    #!perl -slw use strict; use threads; sub one { select('','','',rand(0.01)), print 'one' while 1; } sub two { select('','','',rand(0.01)), print 'two' while 2; } async \&one; two;

    Doing something useful with it requires a little more thought.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re: Parallel trigger of different subs
by karlgoethebier (Monsignor) on Jan 02, 2018 at 17:06 UTC

    See also Threads From Hell.

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Parallel trigger of different subs
by thanos1983 (Vicar) on Jan 02, 2018 at 19:39 UTC

    Hello Anonymous Monk,

    In reply to your merged thread Running 2 different subs simultaneously. I am not sure exactly why the replies of the fellow Monks did not cover your question but maybe the use of Parallel::Simple a really simple module in parallel processes will help you to understand that your question is answered already.

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Parallel::Simple qw(prun); sub myFooSub { say "$$ foo 'clear bgp'"; } sub myBarSub { say "$$ bar 'clear ospf'"; } # style 2: named code blocks (like the Benchmark module) prun( foo => \&myFooSub, bar => \&myBarSub, ) or die( Parallel::Simple::errplus() ); __END__ $ perl test.pl 21120 foo 'clear bgp' 21121 bar 'clear ospf'

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Parallel trigger of different subs
by Anonymous Monk on Jan 02, 2018 at 13:13 UTC
    If you describe your overall goal ... your particular use-case ... more-completely at a place like this, you will get a better answer. Always remember that the CPAN search site is literally stuffed with solutions that you can simply take off-the-shelf and use in your projects – and some Monk somewhere already knows all about the good ones. If you are doing "a thing that has already been done," such as in this case, you can be certain that CPAN will be able to take you very far along your path, very fast. Therefore, always begin there ... and, by asking conceptual questions here.
Running 2 different subs simultaneously
by Anonymous Monk on Jan 02, 2018 at 17:59 UTC

    Sorry, i posted this thread earlier with the Title " Parallel trigger of different sub". I was not specific there. Let me be more specific in this thread. My customer wants to run 2 different subs together.

    just for example. Sub A does "clear bgp" and Sub B does "clear ospf " and he don't want to add all the steps in Sub B to Sub A (and run only Sub A). Both should be different entities . But, he wants to execute both "clear bgp" and "clear ospf" at the same time (not one after the other, should be parallel). This is just an example

      "...not one after the other, should be parallel..."

      Try to add say MCE::Hobo->pid(); to the subs and say $$; say for MCE::Hobo->list_running(); say MCE::Hobo->pending(); to main in the example i provided and watch the flow. Also increase the constant AMOUNT and watch what happens in what ever "task manager" you use. You may also say # use threads; and see what happens.

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1206525]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-06-19 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (114 votes). Check out past polls.

    Notices?