Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Async HTTP Request

by DeliriumNZ (Novice)
on Jul 24, 2011 at 23:11 UTC ( [id://916451]=perlquestion: print w/replies, xml ) Need Help??

DeliriumNZ has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a Async HTTP Request so that it does a GET request to a URL which contains XML. When the XML is modified then it process the modified XML through a function. I am very new to HTTP requests. My example code is:

use HTTP::Async; use HTTP::Request; my $async = HTTP::Async->new; $async->add( HTTP::Request->new( GET => 'http://www.perl.org/' + ) );

I keep getting a error 'HTTP::Async object destroyed but still in use' Any help would be much appreciated

Replies are listed 'Best First'.
Re: Async HTTP Request
by BrowserUk (Patriarch) on Jul 24, 2011 at 23:25 UTC

    You start the get request asynchronouslyand then fall off the end of the script before it has completed,hence the error.

    Try waiting for the response:

    use HTTP::Async; use HTTP::Request; my $async = HTTP::Async->new; $async->add( HTTP::Request->new( GET => 'http://www.perl.org/' ) ); my $response = $async->wait_for_next_response;

    Of course, unless you add several gets, that is the same as doing a synchronous get, but it will 'fix' the error.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Async HTTP Request
by Tanktalus (Canon) on Jul 25, 2011 at 05:05 UTC

    I'm curious. Why HTTP::Async? While it does look to make async calls, I have to admit that I've never seen it before. Not that, by itself, that's a good reason, of course :-) I would have expected the natural method for moving HTTP requests to be asynchronous to be threads. And then there's the new fad going around of using Coro (which I only started using within the last couple months). If you check that link, you'll see I'm actually using AnyEvent::HTTP to do the async HTTP requests (and then doing funny things with them to invert the control back away from call-backs).

    Of course, this necessitates that you adjust (somewhat) to using a different model for your development, whether that be threading (and the fun parts of sharing variables such that they work), or event-based programming in general, or Coro (usually with events). However, the big plus to all of these options, in my opinion, is that they scale - they are methods of development that work well for many things, including, but not limited to, asynchronous HTTP requests. You can do lots of things asynchronously (I'm also doing DB fetch/store asynchronously thanks to Coro::DBI), all with the same mindset. HTTP::Async looks neat, but what you learn from using it won't be directly useful in other situations. And, while I expect HTTP::Async to likely work in a single thread (i.e., a single thread that queues a bunch of requests), it may not mix with Coro or AnyEvent very well.

    Don't change just because I say so (I'm just some random schmo on the internet whose real name you don't know, and, even if you did, it would be meaningless, I'm no merlyn or chromatic or any other author that hangs out here from time to time). I'm just suggesting these as useful additions going forward if you need more async stuff.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://916451]
Approved by GrandFather
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found