in reply to Re: send info between 2 programs
in thread send info between 2 programs
a easy example for threads way, error "Free to wrong Pool" happens when join the thread , why ?our @Task_list; our ($DataQueue1,$DataQueue2); my $ExcelFile='D:\HF_project_perl\Update.xls'; #this use win32:ole to read excel @Task_list=Create_TaskList($ExcelFile); print "Create TaskList"; #Create 2 Queues $DataQueue1-- Main thread to thread1; #$DataQueue2-- +-therad1 to thread2 $DataQueue1 = Thread::Queue->new(); $DataQueue2 = Thread::Queue->new(); my $thr1 = threads->create(\&thread1); print "set up thread1 \n";#for debug my $thr2 = threads->create(\&thread2); print "set up thread2 \n"; #for debug #Assign Task to thread1 by $DataQueue1 for my $i (0..$#Task_list){ $DataQueue1->enqueue($Task_list[$i]); print "send task $i to Thread1 \n"; #for debug print "wait 10s \n"; sleep(10); } #Task completed Flag $DataQueue1->enqueue(undef); print "send undef to stop T1 "; $thr2->join(); $thr1->join(); sub thread1{ my $TaskRef; my $Taskcounter; while ( defined($TaskRef = $DataQueue1->dequeue())) { $Taskcounter++; my $SecID=$TaskRef->{SecID}; my $Date=$TaskRef->{Date}; print "Thread1 finish task $Taskcounter"; print "$SecID $Date \n"; $DataQueue2->enqueue($TaskRef); # notify thread2 to follow the task, Assign Task to thead2 by $DataQue +ue2 print "Send task $Taskcounter to T2 \n";#for debug print "T1 wait 2s \n"; sleep(2); } $DataQueue2->enqueue(undef);#Task completed Flag select(STDOUT); print "T1 stop and inform T2 to stop \n"; #for debug } sub thread2{ my $TaskRef; my $Task_Finished_counter=0; while ( defined($TaskRef = $DataQueue2->dequeue()) ) { print "Thread2 finish task $Task_Finished_counter"; print "$TaskRef->{SecID} $TaskRef->{Date} \n"; #for debug } print "T2 stop \n"; #for debug }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: send info between 2 programs
by frieduck (Hermit) on Mar 17, 2009 at 20:46 UTC |