免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: Perl_Er
打印 上一主题 下一主题

关于perl的多线程&多进程的问题 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2009-07-01 08:57 |只看该作者
没错。如果退出这个循环后,你的脚本就结束了,那么最后的 wait_for_a_kid 是没有必要的。BTW,如果wait_for_a_kid 会 wait 所有已经结束的进程,那么它的名字就起的很不合适。


恩,脚本没有在循环后结束,而下面还有事情要做。所以就的wait没有被wait的进程。名字给个wait_for_all_kids好啦。最重要的是完成了其功能。

另外,再帮忙看看上面那个多线程的。谢谢!
check1234 该用户已被删除
22 [报告]
发表于 2009-07-01 08:59 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
23 [报告]
发表于 2009-07-01 10:33 |只看该作者
帮忙看看那个多线程的为什么会run 'out of memory'

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
24 [报告]
发表于 2009-07-01 11:58 |只看该作者
原帖由 Perl_Er 于 2009-6-30 22:28 发表
测试了一下,运行这个简单的程序会"out  of memory"
#!/usr/bin/perl
use threads;
use strict;

sub prt(){
        print "hello\n";
}


my %thread_hash = ();
my $current_thread = 0;
for(0..10000)
{
         if( $current_thread >= 10 )
         {
                 foreach my $thread(keys %thread_hash )
                 {
                        if($thread->done())

你这个 done 是哪来的?我查了 Perl 5.8.8, 和 Perl 5.10.0 的文档,都没有这个函数。

论坛徽章:
0
25 [报告]
发表于 2009-07-01 14:23 |只看该作者
原帖由 MMMIX 于 2009-7-1 11:58 发表

你这个 done 是哪来的?我查了 Perl 5.8.8, 和 Perl 5.10.0 的文档,都没有这个函数。



请看这里:
http://perldoc.perl.org/Thread.html

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
26 [报告]
发表于 2009-07-01 14:31 |只看该作者
原帖由 Perl_Er 于 2009-7-1 14:23 发表



请看这里:
http://perldoc.perl.org/Thread.html

Thread 和 threads 是两套不同的线程实现,现在已经不鼓励用 Thread 了。更为离奇的是,你使用了 use threads; 但却在其中用 Thread 中的函数。

论坛徽章:
0
27 [报告]
发表于 2009-07-01 14:50 |只看该作者

回复 #26 MMMIX 的帖子

看来是我是混淆了这两个模块,经过修改后是这样,但是还是run out of memory.
#!/usr/bin/perl
use threads;
use strict;

sub prt(){
        print "hello\n";
}


my %thread_hash = ();
my $current_thread = 0;
for(0..2000)
{
         if( $current_thread >= 10 )
         {
                 foreach my $thread(keys %thread_hash )
                 {
                        if($thread->is_joinable())
                                {
                                        $thread->join( );
                                        delete $thread_hash{$thread};
                                }else{
                                        next;
                                }
                 }
         }else{
                $thread_hash{$_} = threads->create(\&prt);
        }
}

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
28 [报告]
发表于 2009-07-01 15:12 |只看该作者
原帖由 Perl_Er 于 2009-7-1 14:50 发表
看来是我是混淆了这两个模块,经过修改后是这样,但是还是run out of memory.
my %thread_hash = ();
my $current_thread = 0;
for(0..2000)
{
         if( $current_thread >= 10 )
         {
                 foreach my $thread(keys %thread_hash )
                 {
                        if($thread->is_joinable())
                                {
                                        $thread->join( );
                                        delete $thread_hash{$thread};
                                }else{
                                        next;
                                }
                 }
         }else{
                $thread_hash{$_} = threads->create(\&prt);
        }
}

你有仔细想过这段代码的逻辑么?例如说 %thread_hash 的 key 是 thread object 么?$current_thread 会增加么?

论坛徽章:
0
29 [报告]
发表于 2009-07-01 15:45 |只看该作者

回复 #28 MMMIX 的帖子

先来个简单的,这个也一样.  out of memory
#!/usr/bin/perl
use threads;
use strict;

sub prt(){
        print "hello\n";
}


my $current_thread;
my @thrs;
for(0..2000)
{
        push @thrs,threads->create(\&prt);
}


foreach(@thrs){
        $_->join();
}

论坛徽章:
0
30 [报告]
发表于 2009-07-01 16:26 |只看该作者

回复 #29 Perl_Er 的帖子

改成这个样子是可以的,每次起10个thread,但是每次都要等这10个完成才能起下一轮的10个,有没有更好的办法了。另外如果每次起得太多,对performance影响会比较大,如何确定同时起多少个比较合适. 谢谢!
#!/usr/bin/perl
use threads;
use strict;
use Data:umper;

sub prt(){
        `echo "hello" >> t.txt`;
}


my $current_thread;
my %thrs = ();
for(1..3000)
{
         $current_thread = scalar (keys %thrs);
         if( $current_thread < 10 )
         {
                $thrs{$_} = threads->create(\&prt);
         }else{
                print "more than 10 threads\n";
                print Dumper \%thrs;
                foreach my $t (keys %thrs){
                                $thrs{$t}->join();
                                delete $thrs{$t};
                }
                $thrs{$_} = threads->create(\&prt);
         }
}
foreach my $t (keys %thrs){
        $thrs{$t}->join();
        delete $thrs{$t};
}

print Dumper \%thrs;

[ 本帖最后由 Perl_Er 于 2009-7-1 16:35 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP