Channel(3pm)
π¦ Channel(3pm) π¦
okay so bestie, this is the Channel(3pm) page, and it comes to you from the User Contributed Perl Documentation β¨ every single detail is staying, we're just giving it the glow-up it deserves π
β¨ NAME β¨
Coro::Channel β message queues π
like... a queue. for messages. that's it, that's the vibe. iconic behavior honestly.
π SYNOPSIS π
use Coro;
$q1 = new Coro::Channel <maxsize>;
$q1->put ("xxx");
print $q1->get;
die unless $q1->size;
π DESCRIPTION π
so a Coro::Channel is literally the equivalent of a unix pipe β and also kinda similar to amiga message ports, iykyk π you put stuff in at one end and read stuff out at the other end. if the capacity of the Channel is maxed out, writers will block β no cap, they just sit there waiting. and both ends of a Channel can be read from and written to by as many coroutines as you want, all concurrently, no fighting, no drama, we love a communal queen π«Ά
and the best part? you don't have to load Coro::Channel manually, bestie. it gets loaded automatically when you use Coro and call the new constructor. it's giving auto-pilot β¨
$q = new Coro:Channel $maxsizeβ makes you a brand new channel with that maximum size you asked for (and it's practically unlimited ifmaxsizeis omitted or is zero π). giving a size of one hands you a traditional channel, like a queue that can only ever hold a single element β which means there's no buffering at all, andputwill just wait around until there's a matchinggetcall to hand it to π. to buffer one element you have to say 2, and so on, and so on. the math is mathing.$q->put ($scalar)β puts the given scalar into the queue. that's the whole thing, bestie. yeet it in there π₯$q->getβ returns the next element from the queue, and if there isn't one yet, it waits. patient queen behavior π$q->shutdownβ shuts down the Channel by pushing a virtual end marker onto it π what does that do? it changes the behaviour of the Channel, so that when it becomes empty (or when it is empty) it returnsundefβ almost as if infinitely manyundefelements had been put into the queue. yes, infinitely many. we said what we said.
specifically: this function wakes up any pending get calls and lets them return undef, and the same thing happens on all future get calls too. size will return the real number of stored elements, though. so like, size is not buying into the drama.
another way to describe the behaviour is that get calls will not block when the queue becomes empty β they just immediately return undef. this means that calls to put will work normally, and the data will be returned on subsequent get calls. so it's not closed-closed, it's more like... emotionally unavailable π₯²
this method is useful to signal the end of data to any consumers, quite similar to an end of stream on, say, a tcp socket. so picture it: you have one or more producers that put data into the Channel, and one or more consumers who get them. when all the producers have finished producing data, a call to shutdown signals that fact to any consumers. communication! teamwork! iconic!
a common implementation uses one or more threads that get from a channel until it returns undef. and to clean everything up: first shutdown the channel, then join the threads. in that order. don't do it backwards, bestie π
$q->sizeβ returns the number of elements waiting to be consumed π
please note that this: perl if ($q->size) { my $data = $q->get; ... } is not a race condition β it just works fine, bestie, you're safe π«Ά
note that the number of elements that wait can actually be larger than $maxsize, because it includes any coroutines waiting to put data into the channel (but not any shutdown condition, so don't count that).
which means the number you get back is precisely the number of calls to get that will succeed instantly and return some data. and calling shutdown has no effect on this number. it just doesn't care. main character energy honestly π
π AUTHOR/SUPPORT/CONTACT π
- Marc A. Lehmann
<schmorp@schmorp.de> http://software.schmorp.de/pkg/Coro.html
and that's the whole page, bestie, you ate it ππ this one came to you from perl v5.40.1, dated 2025-03-21, and it's the Channel(3pm) page β go forth and queue responsibly π§β¨
π¦ 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