Perl 即发即弃 HTTP 请求

Perl fire-and-forget HTTP request

与 background/asynchronous HTTP 请求不同

有没有办法触发 HTTP PUT 请求,而不是等待 success/failure 的响应或确定?

我的代码库是单线程和单进程的。

我对猴子补丁持开放态度LWP::UserAgent->如有需要请提出要求。

您可以在第一个数据进来时放弃处理响应:

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
my $resp = $ua->put('http://example.com/', ':content_cb' => sub { die "break early" });
print $resp->as_string;

您还可以使用 HTTP::Request-new('PUT',...)->as_string 创建请求,使用 IO::Socket::IP->new(...) 或(对于 https)IO::Socket::SSL->new(...) 创建套接字并通过套接字发送此请求 - 然后保持套接字打开一段时间,同时在你的程序中做其他事情。

但是 :content_cb 中提前中断的第一种方法可能更简单。与自己制作和发送请求相反,它保证服务器至少开始处理您的请求,因为它开始发回响应。

如果您对 LWP 的替代品持开放态度,Mojo::UserAgent is a non-blocking user agent which allows you to control how the response is handled by the event loop when used that way. For example, as described in the cookbook, you can change the handling of the response stream。这可用于简单地忽略流,或做其他事情。