求教:线程问题
最近学习线程,编写测试程序manage.pl,利用线程访问srv.list中的服务器,取得uname -a 的值,但是提示
Thread 1 terminated abnormally: Not a CODE reference at manage.pl line 17.
另外如果列表中某台服务器不能访问,程序会中断
并提示
Permission denied at manage.pl line 35
Perl exited with active threads:
0 running and unjoined
6 finished and unjoined
0 running and detached
- #!/usr/bin/perl
- use strict;
- use Net::SSH::Perl;
- use threads;
- use Thread::Queue;
- my $queue = new Thread::Queue;
- my $max = 10;
- open my $IN, '<', 'srv.list' or die "Don't open File!\n";
- while (<$IN>)
- {
- $queue->enqueue($_);
- }
- close $IN;
- for ( my $n = 0; $n < $max; $n++)
- {
- threads->create(\&exec_command());
- }
- foreach my $th (threads->list(threads::all))
- {
- $th->detach();
- }
- sub exec_command()
- {
- my $host = $queue->dequeue();
- my $user = 'zoomi';
- my $pass = 'zoomi-init';
- my $cmd='uname -a';
- my $ssh = Net::SSH::Perl->new($host);
- $ssh->login($user, $pass);
- my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
- if ($exit == 0) {
- print "$host -> $stdout";
- } else {
- print "$host -> $stderr";
- }
- }
复制代码
- 10.60.30.15
- -> Linux zoomi-30-15 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64 GNU/Linux
- Thread 1 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.16
- -> Linux zoomi-30-16 2.6.31-23-server #74-Ubuntu SMP Mon Feb 28 22:32:34 UTC 2011 x86_64 GNU/Linux
- Thread 2 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.17
- -> Linux zoomi-30-17 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64 GNU/Linux
- Thread 3 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.18
- -> Linux zoomi-30-18 2.6.31-14-server #48-Ubuntu SMP Fri Oct 16 15:07:34 UTC 2009 x86_64 GNU/Linux
- Thread 4 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.19
- -> Linux ubuntu 2.6.31-14-server #48-Ubuntu SMP Fri Oct 16 15:07:34 UTC 2009 x86_64 GNU/Linux
- Thread 5 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.25
- -> Linux zoomi-30-25 2.6.31-14-server #48-Ubuntu SMP Fri Oct 16 15:07:34 UTC 2009 x86_64 GNU/Linux
- Thread 6 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.27
- -> Linux server-A-test 2.6.31-14-server #48-Ubuntu SMP Fri Oct 16 15:07:34 UTC 2009 x86_64 GNU/Linux
- Thread 7 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.28
- -> Linux zoomi-30-28 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64 GNU/Linux
- Thread 8 terminated abnormally: Not a CODE reference at manage.pl line 17.
- 10.60.30.29
- -> Linux server-B-test 2.6.31-14-server #48-Ubuntu SMP Fri Oct 16 15:07:34 UTC 2009 x86_64 GNU/Linux
- Thread 9 terminated abnormally: Not a CODE reference at manage.pl line 17.
复制代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
本帖最后由 albertd 于 2011-04-15 02:31 编辑
复制代码Should fix the issues.
那个报错产生的原因是什么?还有我的程序运行后,不能正常退出 。
本帖最后由 ykredrum 于 2011-04-16 22:24 编辑
复制代码\&exec_command()
似乎多了对括号吧
应该是 \&exec_command
对了
最好用个
Thread::Semaphore模块
控制一下线程,确保线程都完成了任务,主线程才退出
不然闪一下就没有了
模块里有具体例子
我现在也在学线程啊。