Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Interrupting HTTP::Request

by tdlewis77 (Sexton)
on Jul 14, 2017 at 00:53 UTC ( [id://1195086]=perlquestion: print w/replies, xml ) Need Help??

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

I need to be able to interrupt a pending HTTP::Request without terminating the process. Thanks to some suggestions from Chatterbox, I now have a snippet of code that works.

$ua->add_handler("response_data", sub {croak() if someCondition(); return 1}); my $resp = $ua->get($url);

In my case someCondition() tests a variable that's set in my INT handler so that it aborts the download if the user presses ^C.

Replies are listed 'Best First'.
Re: Interrupting HTTP::Request
by 1nickt (Canon) on Jul 14, 2017 at 11:23 UTC

    Hi tdelwis77,

    I think what you need to do is fork a child process to do the HTTP request, so that your callback can kill the UA but not the parent process.

    I am not an expert on multiprocess programming, but the code below seems to do what you want, if I understand your need correctly.

    use strict; use warnings; use feature 'say'; use LWP::UserAgent; my $foo = 'bar'; $SIG{'INT'} = sub { $foo = 'baz'; }; my $ua = LWP::UserAgent->new; $ua->add_handler("response_data", sub { if ( $foo eq 'baz' ) { say 'killing UA now'; croak(); } return 1; }); my $url = 'http://ipv4.download.thinkbroadband.com/10MB.zip'; my $filename = "$0.zip"; my $forked = fork // die 'Could not fork!'; if ( $forked == 0 ) { # child say "In child with $$"; my $req = HTTP::Request->new( GET => $url ); my $res = $ua->request( $req, $filename ); exit; } my $done = wait; say "In parent: $done done"; exit;
    (Note that this "aborts the download" but will leave the partially downloaded file on disk, as written with the second arg to $ua->request.)

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Interrupting HTTP::Request
by BrowserUk (Patriarch) on Jul 14, 2017 at 02:13 UTC
    In my case someCondition() tests a variable that's set in my INT handler so that it aborts the download if the user presses ^C.

    And...? There doesn't seem to be a question here?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found