运行以下程序时-t=17 -fn=15,程序会在合理的时间内停止.这意味着最大线程数大于写入和并发缓存的文件数.
按要求更新:$ perl6时的探查器输出 --profile con-test.p6 -t = 7 -fn = 5
当-t> = -fn时程序无法停止!
当程序运行-t=17 -fn=20 --hack程序时,然后在合理的时间内运行完成.
我不知道下面的程序是否失败,因为我在CompUnit中遇到问题,或者是否存在我遗漏的线程(或其他)问题.
假设存在某种线程情况并且文件数必须小于线程数,那么重写hack的最佳方法是什么.目前,数组中充满了Promises,然后允许保留.但等待要求在清空阵列并重新填充之前保留所有这些.我认为这应该被重写为渠道或供应.但是,我无法弄清楚如何做到这一点.
提前致谢.
use v6.c;
use nqp;
use File::Directory::Tree;
my Lock $lock;
my $precomp;
#assume a writable directory files/
sub MAIN(:$t=5, :$fn = 10, :$hack=False ) {
PROCESS::<$SCHEDULER> = ThreadPoolScheduler.new(initial_threads => 0, max_threads => $t);
rmtree '.test';
empty-directory 'files';
my $precomp-store = CompUnit::PrecompilationStore::File.new(prefix => '.test'.IO );
$precomp = CompUnit::PrecompilationRepository::Default.new(store => $precomp-store);
$lock .=new;
my %files = |gather for ^$fn {
my $f = "files/name_$_.pod6";
$f.IO.spurt: data;
take "name_$_" => %(:key(nqp::sha1($f)), :path($f))
}
my @compilations;
my @compiled;
for %files.kv -> $source, (:path($path), :key($key)) {
@compilations.push: start compile( $source, $key, $path );
if $hack {
if @compilations.elems %% ($t - 2) {
@compiled.append: await @compilations;
@compilations = ()
}
}
}
@compiled.append: await @compilations;
for @compiled {
if .<error>.defined {
say .<error>
}
else {
say .<source> ~ ' compiled'
}
}
}
sub compile( $source, $key, $path ) {
my ($handle , $error, $status );
try {
CATCH {
default {
$error = "Compile error in $source:\n\t" ~ .Str
}
}
$lock.protect( {
$precomp.precompile($path.IO, $key, :force );
$handle = $precomp.load($key)[0];
})
}
with $handle {
$status = 'OK';
}
else {
$status = 'Failed';
$error = 'unknown precomp error' without $error; # make sure that $error is defined for no handle
}
%(:$error, :$status, :$source)
}
sub data(-->Str ) {
q:to/DD/
=begin pod :tag<self>
=TITLE Community
X<|Community>
=SUBTITLE Information about the people working on and using Perl 6
=head1 Overview
"Perl 5 was my rewrite of Perl. I want Perl 6 to be the community's rewrite
of Perl and of the community." - Larry Wall
=head1 The Perl 6 community
There is a large presence on the C<#perl6> channel on C<freenode.net>,
who are happy to provide support and answer questions. More resources can be found in the L<perl6.org community page|https://perl6.org/community/>. L<Camelia|https://perl6.org/>, the multi-color butterfly with P 6 in her wings, is the symbol of this diverse and welcoming community. We use extensively
the L<C<#perl6>|https://perl6.org/community/irc> IRC channel for communication, questions and simply hanging out. Check out this L<IRC lingo|http://www.ircbeginner.com/ircinfo/abbreviations.html> resource for the abbreviations frequently used there. L<StackOverflow|https://stackoverflow.com/questions/tagged/perl6> is also a great resource for asking questions and helping others with their Perl 6 problems and challenges.
The Perl 6 community publishes every December an L<Advent Calendar|https://perl6advent.wordpress.com/>, with Perl 6 tutorials every day until Christmas. Organization and assignment of days is done through the different Perl 6 channels and the L<Perl6/mu|https://github.com/perl6/mu> repository. If you want to participate, it starts organization by the end of October, so check out the channels above for that.
=end pod
DD
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果需要太多线程,则程序会挂起,因此解决方法是创建一个队列,分配尽可能多的线程,然后当每个线程中的例程完成时,它将数据从队列中弹出。
\n\n下面我实现了这个策略,它可以处理线程数小于文件数的情况,而不会冻结。实际上,线程数必须比文件数少两个(请参阅 参考资料if +@threads < $t - 2)。将 2 减少到 1 会导致程序再次挂起。不完全确定这是为什么。
以下是用队列重写问题中的程序。
\n\n#!/usr/bin/env perl6\nuse v6.d;\nuse nqp;\nuse File::Directory::Tree;\n\nmy Lock $lock;\nmy $precomp;\n#assume a writable directory files/\n\nsub MAIN(:$t=15, :$fn = 10 ) {\n say "Threads: $t, Files: $fn, Compiler:", $*PERL;\n PROCESS::<$SCHEDULER> = ThreadPoolScheduler.new(initial_threads => 0, max_threads => $t);\n\n rmtree \'.test\';\n empty-directory \'files\';\n\n my $precomp-store = CompUnit::PrecompilationStore::File.new(prefix => \'.test\'.IO );\n $precomp = CompUnit::PrecompilationRepository::Default.new(store => $precomp-store);\n $lock .=new;\n my %files = |gather for ^$fn {\n my $f = "files/name_$_.pod6";\n $f.IO.spurt: data;\n take "name_$_" => %(:key(nqp::sha1($f)), :path($f))\n }\n my @compilations;\n my @compiled;\n my @threads; \n for %files.kv -> $source, (:path($path), :key($key)) {\n @compilations.push: ( $source, $key, $path );\n @threads.push( start\n sub ( @queue ) {\n my @params = @queue.pop.list if @queue;\n return unless +@params;\n my $res = compile( |@params );\n $lock.protect({\n @compiled.append: $res;\n });\n &?ROUTINE( @queue )\n }( @compilations )\n ) if +@threads < $t - 2;\n }\n await @threads;\n for @compiled {\n if .<error>.defined {\n say .<error>\n }\n else {\n say .<source> ~ \' compiled\'\n }\n }\n}\n\nsub compile( $source, $key, $path ) {\n my ($handle , $error, $status );\n try {\n CATCH {\n default {\n $error = "Compile error in $source:\\n\\t" ~ .Str\n }\n }\n $precomp.precompile($path.IO, $key, :force );\n $handle = $precomp.load($key)[0];\n }\n with $handle {\n $status = \'OK\';\n }\n else {\n $status = \'Failed\';\n $error = \'unknown precomp error\' without $error; # make sure that $error is defined for no handle\n }\n %(:$error, :$status, :$source)\n}\n\nsub data(-->Str ) {\n q:to/DD/\n =begin pod :tag<self>\n\n =TITLE Community\n X<|Community>\n\n =SUBTITLE Information about the people working on and using Perl\xc2\xa06\n\n =head1 Overview\n\n "Perl\xc2\xa05 was my rewrite of Perl. I want Perl\xc2\xa06 to be the community\'s rewrite\n of Perl and of the community." - Larry Wall\n\n =head1 The Perl\xc2\xa06 community\n\n There is a large presence on the C<#perl6> channel on C<freenode.net>,\n who are happy to provide support and answer questions. More resources can be found in the L<perl6.org community page|https://perl6.org/community/>. L<Camelia|https://perl6.org/>, the multi-color butterfly with P 6 in her wings, is the symbol of this diverse and welcoming community. We use extensively\n the L<C<#perl6>|https://perl6.org/community/irc> IRC channel for communication, questions and simply hanging out. Check out this L<IRC lingo|http://www.ircbeginner.com/ircinfo/abbreviations.html> resource for the abbreviations frequently used there. L<StackOverflow|/sf/ask/tagged/perl6/> is also a great resource for asking questions and helping others with their Perl\xc2\xa06 problems and challenges.\n\n The Perl\xc2\xa06 community publishes every December an L<Advent Calendar|https://perl6advent.wordpress.com/>, with Perl\xc2\xa06 tutorials every day until Christmas. Organization and assignment of days is done through the different Perl\xc2\xa06 channels and the L<Perl6/mu|https://github.com/perl6/mu> repository. If you want to participate, it starts organization by the end of October, so check out the channels above for that.\n\n =end pod\n\n DD\n}\nRun Code Online (Sandbox Code Playgroud)\n