comparison lib/threader.pm @ 0:023c380900ef draft default tip

Init repository with last massbank_ws_searchspectrum master version
author fgiacomoni
date Wed, 19 Apr 2017 11:31:58 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:023c380900ef
1 package lib::threader ;
2
3 use strict;
4 use warnings ;
5 use Exporter ;
6 use Carp ;
7 use threads;
8 use threads::shared;
9 use Thread::Queue;
10 use diagnostics ;
11 use Data::Dumper ;
12 use Carp qw (cluck croak carp) ;
13 use LWP::UserAgent;
14 use LWP::Simple ; ## Lib de protocole HTTP de download
15 use SOAP::Lite + trace => qw(fault); ## SOAP for web service version 0.67
16 import SOAP::Data qw(name);
17
18 use Data::Dumper ;
19
20 use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS);
21
22 our $VERSION = "1.0";
23 our @ISA = qw(Exporter);
24 our @EXPORT = qw(threading_getRecordInfo);
25 our %EXPORT_TAGS = ( ALL => [qw(threading_getRecordInfo )] );
26
27 =head1 NAME
28
29 My::Module - An example module
30
31 =head1 SYNOPSIS
32
33 use My::Module;
34 my $object = My::Module->new();
35 print $object->as_string;
36
37 =head1 DESCRIPTION
38
39 This module does not really exist, it
40 was made for the sole purpose of
41 demonstrating how POD works.
42
43 =head1 METHODS
44
45 Methods are :
46
47 =head2 METHOD new
48
49 ## Description : new
50 ## Input : $self
51 ## Ouput : bless $self ;
52 ## Usage : new() ;
53
54 =cut
55
56 sub new {
57 ## Variables
58 my $self={};
59 bless($self) ;
60 return $self ;
61 }
62 ### END of SUB
63
64 =head2 METHOD searchSpectrumWorker
65
66 ## Description : work with searchSpectrum method in threading mode
67 ## Input : $Qworks
68 ## Output : $results
69 ## Usage : my ( $results ) = searchSpectrumWorker( $Qworks ) ;
70
71 =cut
72 ## START of SUB
73 sub searchSpectrumWorker {
74 my $self = shift;
75 my ($Qworks, $server, $ion_mode, $instruments, $max, $unit, $tol, $cutoff) = @_ ;
76 my @results = () ;
77 my @fake = () ;
78
79 my $omassbank = lib::massbank_api->new() ;
80 my $soap = $omassbank->selectMassBank($server) ;
81
82 while(my $pcgroup = $Qworks->dequeue) {
83 # print Dumper $pcgroup ;
84 my $oquery= lib::massbank_api->new() ;
85 my ($result, $num) = eval{$oquery->searchSpectrum($soap, $pcgroup->{'id'}, $pcgroup->{'mzmed'}, $pcgroup->{'into'}, $ion_mode, $instruments, $max, $unit, $tol, $cutoff) ; } or die;
86 # print "The query send to massbank return $num entries...\n" ;
87 # print Dumper $result ;
88
89 if (!defined $num) { $num = 0 ; }
90 if ($num >= 0 ) {
91 push @results, $result ;
92 }
93 else {
94 push @fake, $pcgroup ;
95 }
96 }
97 return (@results) ;
98 }
99 ## END of SUB
100
101
102 =head2 METHOD threading_getRecordInfo
103
104 ## Description : prepare parallel threads - DEPRECATED
105 ## Input : $soap, $list
106 ## Output : $results
107 ## Usage : my ( $results ) = threading_getRecordInfo( $soap, $list ) ;
108
109 =cut
110 ## START of SUB
111 sub threading_getRecordInfo {
112 ## Retrieve Values
113 my $self = shift ;
114 my ( $osoap, $list ) = @_ ;
115
116 my @results = () ;
117 my $i = 0 ; # position in the ids list
118
119 if ( ( defined $list ) ) {
120
121 my $oquery = lib::massbank_api->new() ;
122
123 for (my $i = 0; $i < (scalar @{$list}); $i++ ) {
124 my $thr = threads->create( sub { $oquery->getRecordInfo($osoap, $list->[$i]) } ) ;
125 push ( @results, $thr->join ) if $list->[$i] ;
126 }
127 }
128 else {
129 warn "Your input list of ids is undefined\n" ;
130 }
131 return(\@results) ;
132 }
133 ## END of SUB
134
135
136 =head2 METHOD threading_searchSpectrum
137
138 ## Description : prepare parallel threads - DEPRECATED
139 ## Input : $soap,
140 ## Output : $results
141 ## Usage : my ( $results ) = threading_searchSpectrum( $soap, ) ;
142
143 =cut
144 ## START of SUB
145 sub threading_searchSpectrum {
146
147 ## http://www.perlmonks.org/?node_id=735923
148 ## http://www.nntp.perl.org/group/perl.ithreads/2003/05/msg696.html
149 ## http://stackoverflow.com/questions/15222480/web-service-using-perl-wsdl-and-multi-threading-does-not-working
150 ## Retrieve Values
151 my $self = shift ;
152 my ( $osoap, $pcgroup_list, $pcgroups, $ion_mode, $instruments, $max, $unit, $tol, $cutoff ) = @_ ;
153
154 my @results = () ;
155 my $n = 6 ; # position in the ids list
156
157 if ( ( defined $pcgroups ) ) {
158
159 print Dumper $pcgroups ;
160
161 my $oquery = lib::massbank_api->new() ;
162
163 foreach my $pc (@{$pcgroup_list}) {
164
165 print "\t---> Create a thread for pcgroup n-$pc\n" ;
166
167 my $thr = threads->create(
168 sub {
169 $oquery->searchSpectrum($osoap, $pcgroups->{$pc}{'mzmed'}, $pcgroups->{$pc}{'into'}, $ion_mode, $instruments, $max, $unit, $tol, $cutoff) ;
170 }
171 ) ;
172 push ( @results, $thr->join ) ;
173 } ## end foreach
174 }
175 else {
176 warn "Your input list of ids is undefined\n" ;
177 }
178 return(\@results) ;
179 }
180 ## END of SUB
181
182
183 =head2 METHOD thread_and_queue_searchSpectrum
184
185 ## Description : prepare parallel and queuing threads - DEPRECATED
186 ## Input : $soap,
187 ## Output : $results
188 ## Usage : my ( $results ) = thread_and_queue_searchSpectrum( $soap, ) ;
189
190 =cut
191 ## START of SUB
192 sub thread_and_queue_searchSpectrum {
193 # my $self = shift;
194 # my ( ) = @_;
195 #
196 # our $THREADS = 10;
197 # my $Qwork = new Thread::Queue;
198 # my $Qresults = new Thread::Queue;
199 #
200 # ## Create the pool of workers
201 # my @pool = map{
202 # threads->create( \&worker, $Qwork, $Qresults )
203 # } 1 .. $THREADS;
204 #
205 # ## Get the work items (from somewhere)
206 # ## and queue them up for the workers
207 # while( my $workItem = getWorkItems() ) {
208 # $Qwork->enqueue( $workItem );
209 # }
210 #
211 # ## Tell the workers there are no more work items
212 # $Qwork->enqueue( (undef) x $THREADS );
213 #
214 # ## Process the results as they become available
215 # ## until all the workers say they are finished.
216 # for ( 1 .. $THREADS ) {
217 # while( my $result = $Qresults->dequeue ) {
218 #
219 # ## Do something with the result ##
220 # print $result;
221 # }
222 # }
223 #
224 # ## Clean up the threads
225 # $_->join for @pool;
226 }
227 ## END of SUB
228
229
230
231 1 ;
232
233
234 __END__
235
236 =head1 SUPPORT
237
238 You can find documentation for this module with the perldoc command.
239
240 perldoc XXX.pm
241
242 =head1 Exports
243
244 =over 4
245
246 =item :ALL is ...
247
248 =back
249
250 =head1 AUTHOR
251
252 Franck Giacomoni E<lt>franck.giacomoni@clermont.inra.frE<gt>
253
254 =head1 LICENSE
255
256 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
257
258 =head1 VERSION
259
260 version 1 : xx / xx / 201x
261
262 version 2 : ??
263
264 =cut