LWP(3pm)
π NAME π
Coro::LWP β bestie, it makes LWP non-blocking... as much as it possibly can π«Ά
β¨ SYNOPSIS β¨
use Coro::LWP; # afterwards LWP should not block
That's it. That's the whole ritual. One use, and boom π
π¦ ALTERNATIVES π¦
Okay so lowkey? Over the years a bunch of less invasive alternatives have popped up, and ngl you might find some of them way more acceptable than this one β because this module is, and I say this with love, rather invasive and fragile. π Every single one of these only supports HTTP (and sometimes HTTPS), just so you know.
AnyEvent::HTTPβ works totally fine without Coro, no cap. But it requires a very different API than LWP. Probably the best choice iff you can deal with a completely different event-based API.LWP::Protocol::AnyEvent::httpβ makes LWP useAnyEvent::HTTP. It does not make LWP event-based, but it does let Coro threads schedule unimpeded through its AnyEvent integration. And the best part? You get to use the LWP API normally. Iconic πLWP::Protocol::Coro::httpβ basically the same as the one above, distinction unclear. :) (her words, not mine π)AnyEvent::HTTP::LWP::UserAgentβ a whole different user agent implementation. Not completely transparent to users, and it requires Coro.
πΈ DESCRIPTION πΈ
This module is an AnyEvent user, so you need to make sure you use and run a supported event loop. Periodt.
So here's the vibe: this module tries to make LWP non-blocking with respect to other coroutines, as much as possible, and with whatever means it takes. π€
And the thing is β LWP really tries very hard to be blocking (it relies on a whole lot of undocumented functionality in IO::Socket), so this module had to be super invasive and it must be loaded very early to take the proper effect. Load it late and it's giving nothing, bestie. π
Also note: the module AnyEvent::HTTP might offer an alternative to the full LWP that's actually designed to be non-blocking. Just saying. π
Here's what it currently does (future versions of LWP might require different tricks, iykyk):
- It loads
Coro::Select, overwriting the perlselectbuiltin globally.- This is necessary because LWP calls
selectquite often for timeouts and who-knows-what. - Impact: everybody else uses this (slower) version of
select, too. It should be quite compatible with perl's builtinselect, though.
- This is necessary because LWP calls
- It overwrites
Socket::inet_atonwithCoro::Util::inet_aton.- This is necessary because LWP might (and does) try to resolve hostnames this way.
- Impact: some code might not expect coroutine semantics. For example, when you
forkyou might prefer the blocking variant, because other coroutines shouldn't actually run.
- It replaces the base class of
Net::HTTP,Net::FTP,Net::NNTP.- This is necessary because LWP does not always use
selectto see whether a filehandle can be read/written without blocking, so the base classIO::Socket::INETneeds to be replaced byCoro::Socket. - Impact:
Coro::Socketis not at all compatible withIO::Socket::INET. While it duplicates some undocumented functionality required by LWP, it does not have all the methods ofIO::Socket::INETand might act quite differently in practise. Also, protocols other than the ones mentioned above will still block, at least some of the time. π
- This is necessary because LWP does not always use
All this likely makes other libraries than just LWP not block, but that's just a side effect you cannot rely on. π«
"Increases parallelism" is not supported by all libraries β some might cache data globally. π
π AUTHOR/SUPPORT/CONTACT π
- Marc A. Lehmann
<schmorp@schmorp.de> http://software.schmorp.de/pkg/Coro.html
Okay that's the whole page bestie, you ate it up π This lil masterpiece ships with perl v5.40.1, dated 2025-03-21, and it's part of the User Contributed Perl Documentation β go forth and be non-blocking, you main character ππ
π¦ where this page comes from
- debian trixie: libcoro-perl 6.570-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcoro-perl 6.570-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms