Thread sample
Said Perl6 in recent years has been in the propaganda Promise Ah, Supply ah concurrent programming, but the API changes too fast, in the middle of the 2013 Jnthn speech in the async use of the presentation, now directly reported that this function does not exist, seems to be changed to start? God knows when it's going to change. So use the bottom Thread and Channel to write it. In fact, this is my first time to write Thread.
The code is as follows |
Copy Code |
Use V6; Class OpenSSH { has $!user = ' root '; has $!port = 22; Has $!ssh = "Ssh-ostricthostkeychecking=no-l{$!user}-p{$!port}"; Multi method exec ($host, $cmd) { My $out; My $shell = $!ssh ~ $host ~ ' ~ $cmd; try {$out = qqx{$shell}.chomp} CATCH {Note ("Failed: $!")}; return $out; } Multi method exec (@hosts, $cmd) { my $c = channel.new; My @t = @hosts. Map ({ Thread.Start ({ My $r = $.exec ($_, $cmd); $c. Send ($r); }) }); @t>>.finish; Return @hosts. Map: {$c. Receive}; } } My $ssh = openssh.new (user => ' root '); Say $ssh. EXEC (' 10.4.1.21 ', ' uptime '); My @hosts = ' 10.4.1.21 ' xx 5; My @ret = $ssh. EXEC (@hosts, ' Sleep 3;echo $$ '); Say @ret. Perl; |
Very rudimentary code. First one is to confirm SSH without password login, because there is no write Expect, followed by no ThreadPool, so the concurrent operation can not be too fierce, will twist the waist.
Here are a few places to demonstrate:
Definition and usage of class definitions and attr
The use of Try-catch
You can also not write a try, direct CATCH {}
Usage of qqx{}
This is a relatively large change in the place, QQX can only use {} can not use other characters right. PERL6 provides additional shell () instructions to return the Proc::status object. But this object is actually a status code, does not include standard output, error output or anything.
Usage of String Connector ~
Definition and usage of multi method
Definition and usage of function signature, optional parameter and named parameter definition and usage see the next section.
Usage of >> operator
This is actually the equivalent of. Finish for @t. This strange operator is said to be able to line up the array operation when possible, so the order of return is not the same as. Map.
Usage of the XX operator
There are X operators in the PERL5, and Perl6, X and Z are added to the operator. They are character expansion arrays, array extensions into multidimensional arrays, and most groups of compressed single arrays (i.e., zip operations).
Usage of Channel and Thread objects
In the roast test suite, there are only thread and lock test cases. Semaphore actually support (because MOARVM is based on LIBUV, LIBUV support it, of course), but even test cases are not written ...
The default concurrent programming takes the Threadpoolscheduler class, a little bit, and the default set number of threads is 16. Consider whether the next step is to refine my little script, or learn Supply or Promise to see how it is used.
Interested in using Libssh2 's children's shoes, you can learn how to use Nativecall.
Threadpoolscheduler sample
Based on the contents of the S17-concurrency document, several lines of script have been rewritten to achieve the ThreadPool effect:
The code is as follows |
Copy Code |
Multi method exec (@hosts, $cmd,: $parallel = +) { my $c = CHANNEL.N ew My $s = threadpoolscheduler.new (max_threads => $parallel); @hosts. Map ({ $s. Cue ({ My $r = $.exec ($_, $cmd); $c. Send ($r); }) }); return @hosts. Map: {$c. Receive}; } |
Here the default concurrency value is changed to 16, consistent with Rakudo. If you don't need to be adjustable, you can actually write $*scheduler.cue ({}) directly.
Then the call method also corresponds to modify, taking into account the identification, the concurrent value to the named parameter. The method is invoked as follows:
The code is as follows |
Copy Code |
My @hosts = slurp (' iplist.txt '). Lines; My @ret = $ssh. EXEC (@hosts, ' Sleep 3;echo $$ ',:p arallel (5));
|
Running can be seen, although the Iplist.txt put 40 IP, but the concurrent SSH only 5.
Promise sample
Continue, the next section of the S17 content is Promise, the previous blog has mentioned several PERL5 promises modules or similar things (such as mojo::ioloop::D elay), including JavaScript, etc. also have the same name.
However, Perl5 's promises thinking refers to Scala, the grammar is biased towards Nodejs and Golang (all with a call to create the defer object promises), written with the Perl6 of the original Promise gap is larger.
Consider SSH this scenario may not be very used. Process control such as Promise. In,. Then,. anyof (especially. In this is not necessarily possible, because the Promise is also used at the bottom of $*scheduler.cue (), and this in the MOARVM is still Does not support: In/:at/:every and other parameters, directly show the simplest concurrency:
code is as follows |
copy code |
Multi method exec (@hosts, $cmd,: $parallel =) { $*scheduler = Thr Eadpoolscheduler.new (max_threads => $parallel); await @hosts. Map: { Start { $.exec ($_, $cmd); }; }; } |
In simple terms, each start {&c} creates a Promise object that is automatically $p according to &c's return value. Keep ($result) or $p. Break (Exception). Then await (*@p) reclaims the results of all Promise.
This directly modifies the $*scheduler, which is a global variable, the scheduling method of the current process. The Promise class uses this variable by default. If you want to use $s like the previous section, you cannot use start {} but instead use Promise.start ({}, $s). It's obviously not very beautiful to write.
Supply sample
Supply is responsive programming, similar to the reactive concept in Java. What should fit is one thing multiple processes repeat. The scene is not very right, and the present S17 is not complete, will not write.