Note that there are some explanatory texts on larger screens.

plurals
  1. POThread "tagging" in Perl for async HTTP request
    text
    copied!<p>I know there are other classes I can use, however I want to use LWP. The accepted answer here (<a href="https://stackoverflow.com/questions/8372723/how-do-i-make-parallel-http-requests-in-perl-and-receive-them-back-in-order">How do I make parallel HTTP requests in Perl, and receive them back in order?</a>) does what I want however I need to "tag" the threads so that I can match the request with the response. I have modified the code to the following:</p> <pre><code>#Setup and query the DB: #hold all response\id pairs my @response_bundles; while (my @row = $query-&gt;fetchrow_array()) { my $id = $row[0]; my $url = $row[1]; push @threads, async { my @container_hash; $container_hash[0] = @row; $container_hash[1] = $ua-&gt;get($url); push @response_bundles, @container_hash; }; } #Once this loop terminates all threads are done for my $thread (@threads) { $thread-&gt;join; } #Since all threads are completed we can now sort through the response bundles for my $response_bundle (@response_bundles) { print "test\n"; } </code></pre> <p>My goal is to kick off a bunch of HTTP requests and store their ($id, $response) pair in an array. I push them all into an async process and the sub in async{} SHOULD be doing this (but it's not). I loop through the thread array and once that is complete all threads should be done. I then go through my bundle and do things however the "print test" is never firing. Am I thinking about this wrong?</p> <p>EDIT:</p> <p>Per the comment I tried returning the value but this does not work either</p> <pre><code>while (my @row = $query-&gt;fetchrow_array()) { my $id = $row[0]; my $url = $row[1]; push @threads, async { my @container_hash; $container_hash[0] = @row; $container_hash[1] = $ua-&gt;get($url); return @container_hash; }; } #Once this loop terminates all threads are done for my $thread (@threads) { my @container; @container = $thread-&gt;join; print $container[1]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload