Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Interrupting HTTP::Request

by tdlewis77 (Sexton)
on Jul 14, 2017 at 00:53 UTC ( #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 (Monsignor) 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 = ''; my $filename = "$"; 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 (Pope) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1195086]
Front-paged by Corion
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2018-02-24 03:40 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.