LWP::Parallel callback question

LWP::Parallel callback question

am 28.01.2006 06:43:43 von robert.bradley

In the README I see the following:

"You can define callback routines which will be called whenever a
connection is established, is cut off, new data comes in or a request
finishes. The subroutines can be global for all requests you
registered, or different for every single request. "

This sounds like it is possible to register individual requests with
individual callbacks that get called when the request finishes. But,
it seems the only kind of callback that gets called when a request
finishes is global when we override the on_return method; it is called
for all request that are registered. The individual callbacks for each
request get invoked as data trickles back in, and I have to concatenate
it. I don't see any way to register an individual callback for a given
http request, that only gets called when the request finishes.

Is there some way to get a different callback for each request to only
be invoked once when the corresponding request completes? Or do I need
to write some code to implement this behavior? I don't want to go off
and re-invent the wheel in case I'm just missing the obvioius.

Any advice appreciated.

Re: LWP::Parallel callback question

am 28.01.2006 07:41:44 von John Bokma

robert.bradley@gmail.com wrote:

> In the README I see the following:
>
> "You can define callback routines which will be called whenever a
> connection is established, is cut off, new data comes in or a request
> finishes. The subroutines can be global for all requests you
> registered, or different for every single request. "
>
> This sounds like it is possible to register individual requests with
> individual callbacks that get called when the request finishes. But,
> it seems the only kind of callback that gets called when a request
> finishes is global when we override the on_return method; it is called
> for all request that are registered. The individual callbacks for each
> request get invoked as data trickles back in, and I have to concatenate
> it. I don't see any way to register an individual callback for a given
> http request, that only gets called when the request finishes.
>
> Is there some way to get a different callback for each request to only
> be invoked once when the corresponding request completes? Or do I need
> to write some code to implement this behavior? I don't want to go off
> and re-invent the wheel in case I'm just missing the obvioius.

What I did some time ago is using each request I created (and registered
with pua) as a hash key, with the value an object having a callback
handler associated with the request, and then something like:

my $entries = $pua->wait();

for my $key ( keys %$entries ) {

my $response = $entries->{$key}->response;
my $action = $key2action{ $key };

$action->process( $response .... );
:
:
}

I have no idea if this is *the* way (I consider it quite clumsy), but
couldn't find a better solution in a short time, so I stuck with it.


--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

Re: LWP::Parallel callback question

am 28.01.2006 18:34:25 von robert.bradley

Thanks John, I'm already creating a hash of the requests, so that would
be easy enough. Another downside of this solution might be that the
notification of the callback doesn't happen until the last (and longest
request) completes...