in reply to Threading Web Requests with LWP
Is there an easy way to make all the LWP HTTP requests thread and return back without having to rewrite a large portion of my code?
In theory, where you currently have something like:
... my $content; my $response = $ua->get('http://search.cpan.org/'); if ($response->is_success) { $content = $response->decoded_content; # or whatever } else { warn $response->status_line; } ...
Simply add:
use threads; ... my $content; my $thread = async{ my $response = $ua->get('http://search.cpan.org/'); if ($response->is_success) { return $response->decoded_content; # or whatever } else { return undef; } }; ... if( $thread->is_joinable() ) { $content = $thread->join; if( $content ) { #do domething with it } else { # notify the failure? } }
It will probably end up being slightly more complex than that once you handle errors, but it shouldn't need to be much more so.
If you post (the relevant part of) your code, I might be able to be a little more specific.
|
|---|