免费注册 查看新帖 |

Chinaunix

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

A Question about ithreads..... [复制链接]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-09 09:25 |只看该作者 |倒序浏览
#------------------------------------------------------
最近上面長官要我用multithread的方式將我寫的一個很耗時的程序
加快執行效率,但是我用perl 5.8 的ithread後,發現他在thread間
無法傳送handle
Error如下:
thread failed to start: DBD::Informix::db prepare failed: handle 2
is owned by thread 2003af28 not current thread 20557548
(handles can't be shared between threads and your driver
may need a CLONE method added) at thread.pl line 58.
我原本的想法是利用Thread:ueue由main thread將資料放入輸入
的queue中,然後由前三個thread將queue中的資料取出,並執行
一些測試動作後,放入輸出的queue中。最後由第四個threads將輸出的
queue中的數據,回寫入資料庫中...但是如上面錯誤所見..他建議我要用
CLONE method..不知道有沒有人知道怎麼樣clone?
我很少寫clone方面的東西...有點搞不清楚怎樣 clone..




#!/usr/bin/perl
use threads qw(yield);
use threads::shared;
use Thread:ueue;

my $stream_1 = new Thread:ueue;
my $stream_2 = new Thread:ueue;

my $qdatabase = "XXXXX\@"; # 數據庫的名稱

my $dbh=&DB_CON($qdatabase);

print "ya!!";
#$stream_3 = new Thread:ueue;
$thr1 = threads->;new(\&sub2,1,$stream_1,$stream_2);
print "oh!!";
$thr2 = threads->;new(\&sub2,2,$stream_1,$stream_2);
$thr3 = threads->;new(\&sub2,3,$stream_1,$stream_2);
$thr4 = threads->;new(\&sub3,4,$stream_2,$dbh);

my $sql="select * from test";
my $sth_s = $dbh->;prepare(qq{ $sql })|| die "can't preapre $!";
   $sth_s->;execute || die "can't execute $!";
my $count = 0;

while(my @aa=$sth_s->;fetchrow_array()){
  print "XXXX$count : @aa\n";
  $cmd =  join("##",@aa);
  $stream_1->;enqueue($cmd);
  $count++;
#  last if (++$count==10);
}

$stream_1->;enqueue(undef);
$stream_1->;enqueue(undef);
$stream_1->;enqueue(undef);
$stream_2->;enqueue(undef);
$thr1->;join;
$thr2->;join;
$thr3->;join;
$thr4->;join;


#------------------------------------------------------
sub sub3{
my ($num,$ostream,$dbh ) = @_;

if(defined $dbh){
   my $sql="select * from test";
   my $sth=$dbh->;prepare(qq { $sql } ) || die "can't prepare: $!";
   $sth->;execute || die "can't execute: $!";
   while(my @aa=$sth->;fetchrow_array){
     print "$num : @aa\n";
   }
}
}

sub sub2{
  my ($num,$istream,$ostream) = @_;
#print "Success!!\n";
  while(my $data=$istream->;dequeue){
     print "$num : $data \n";
     $ostream->;enqueue($data);
  }
}


sub DB_CON
{
  my ($qdatabase) = @_;
  use DBI;
  $ENV{"INFORMIXDIR"}="/usr/informix";
  $ENV{"INFORMIXSERVER"}="abcd";
  if ($qdatabase =~m"L123"
  {
    $ENV{"DB_LOCALE"}="en_US.8859-1";
    $ENV{"CLIENT_LOCALE"}="en_US.8859-1";
  }
  else
  {
    $ENV{"DB_LOCALE"}="zh_TW.big5";
    $ENV{"CLIENT_LOCALE"}="zh_TW.big5";
  }
  my $dbh = DBI->;install_driver('Informix');

  my $user="abc";
  my $pwd="cdef";

  my $dbh = DBI->;connect("dbi:Informixqdatabase",$user,$pwd)
         || die ("Can't connect to $qdatabase";
  return $dbh;
}

论坛徽章:
0
2 [报告]
发表于 2003-05-09 13:01 |只看该作者

A Question about ithreads.....

真是烦恼,我看到clone的资料了,可惜忘了哪本书了,好像是很老的perl5的高级开发指南吧。多线程的问题是不能共享一个handdle

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
3 [报告]
发表于 2003-05-09 14:41 |只看该作者

A Question about ithreads.....

現在我想用其他方法..把數據都寫到File中..再用另一個process
將這些File中的資料寫入資料庫內...
我查了看了很多文件..看起來目前無解...
等5.10.0出來看會不會好點..

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2003-05-09 15:50 |只看该作者

A Question about ithreads.....

剛剛研究了一下發現應該是Informix的DBD沒有multithread
所要的Clone的subroutine..
因為IO::File與IO::Soket產生的handle..
都可以跨thread傳送....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP