免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1658 | 回复: 5
打印 上一主题 下一主题

求助 Perl多线程:在线程中dbh shared 出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-28 20:46 |只看该作者 |倒序浏览
#!/bin/perl
use IO::Socket;
use threads;
use threads::shared;
use DBI();
use strict;

#---------------------------------------------------------
#DB connection
my ($url,$user,$passwd)=("DBI:mysql:database=logserver;port=3306;host=127.0.0.1","root","123qwe");
our $dbh=DBI->connect($url,$user,$passwd) or die "can't connect DB mysql ".DBI->errstr;
my $ipHead="192.168.4.";
my (@ips,@iplist):shared;
our @logtype:shared=('test');
for(my $i=1;$i<255;$i++){push(@ips,$ipHead."$i");}

my @threads;
print localtime."\n";
for(my $j=0;$j<@ips;$j++){$threads[$j]=threads->new(\&portScaner,$ips[$j]);}
foreach my $threadinstance(@threads){$threadinstance->join();}
print localtime."\n";

#---------------------------------------------------------
#scan if the port 22 is open
sub portScaner{
my ($ip)=(shift);
my $result=IO::Socket::INET->new("$ip:22");
if($result){
push(@iplist,$ip);
foreach my $type(@logtype){&getLogtypeContext($type);}

$result->close;
}
#else {writeLog("ip=$ip port 22 is not open!\n");}
}
#---------------------------------------------------------
#get filepath keywords splitwords in logtype
sub getLogtypeContext{
my ($logtype)=@_;
my $sth=$dbh->prepare("select filepath,keyword,splitword from t_logtype where type='$logtype';") or die $dbh->errstr;
$sth->execute();
my (@filepath,@keyword,@splitword);
while(my $ref=$sth->fetchrow_hashref())
{
        push(@filepath,split(",",$ref->{filepath}));
        push(@keyword,split(",",$ref->{keyword}));       
        push(@splitword,split(",",$ref->{splitword}));             
}
return (\@filepath,\@keyword,\@splitword);
}

论坛徽章:
0
2 [报告]
发表于 2013-04-28 22:45 |只看该作者
perl的线程还是不要用吧。
use processes instead,  open a new dbh connection in each forked process.

论坛徽章:
0
3 [报告]
发表于 2013-04-29 07:41 |只看该作者
兰花仙子 发表于 2013-04-28 22:45
perl的线程还是不要用吧。
use processes instead,  open a new dbh connection in each forked process.

已经不用这样了:)
有现成的AnyEvent:BI可以用了。AnyEvent:BI实际上就是fork以后waid child pid

论坛徽章:
0
4 [报告]
发表于 2013-04-29 14:19 |只看该作者
谢谢,网上说可以使用Coro,不知道可不可以。
回复 3# anyevent


   

论坛徽章:
0
5 [报告]
发表于 2013-04-29 15:38 |只看该作者
回复 4# swgaylz2008

什么网上说啊...那就是我说的
你可以Coro+AnyEvent:BI,Coro是多线程模型,AnyEvent:BI是非阻塞的DBI版本
   

论坛徽章:
0
6 [报告]
发表于 2013-04-29 20:20 |只看该作者
呵呵,谢谢!实在不好意思啊!{:3_201:} 我做毕业设计要用多线程,一个流程已经写完了,可是加上多线程就各种出错,苦恼!回复 5# anyevent


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP