EV(3pm)

πŸ’– Coro::EV β€” do events the coro-way, with EV πŸ’–

ok so bestie, this is a User Contributed Perl Documentation page for EV(3pm) and it's genuinely so good, sit down, we're doing events the coro-way. ✨


🌸 NAME 🌸

Coro::EV β€” do events the coro-way, with EV πŸ¦‹


✨ SYNOPSIS ✨

use Coro;
use Coro::EV;

EV::READ & Coro::EV::timed_io_once $fh, EV::READ, 60
   or die "timeout\n";

EV::run;

like. that's it. that's the whole vibe. πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

okay so this module is doing two things at once, main character energy fr:

  1. it hands you some utility functions that might be useful for threads β€” tho ngl, Coro::AnyEvent offers more and more portable functions, so keep that in mind πŸ‘€
  2. it integrates Coro into the EV main loop πŸŽ€

Here's the tea on how the integration works: before the process blocks (in EV::run) to wait for events, this module will schedule and run all ready (= runnable) threads of the same or higher priority. After that, it will cede once to a thread of lower priority, then continue in the event loop.

So what does that actually mean for you? Threads with the same or higher priority as the threads running the main loop will inhibit event processing 😀 β€” meanwhile threads of lower priority will get the CPU, but they cannot completeley inhibit event processing. (yes, that typo is in the original, we respect the lore πŸ™ˆ)

⚠️ Note that for that to work you actually have to run the EV event loop in some thread. no mainloop, no magic, bestie.

🌈 RUNNING WITH OR WITHOUT A MAINLOOP 🌈

In general? You should always run EV::run β€” either in your main program, or in a separate coroutine. Periodt.

If you don't do that and all coroutines start waiting for some events, this module will run the event loop once, but lowkey this is very inefficient and it will also not make it possible to run background threads. it's giving "technically works, spiritually a mess" πŸ’€

To run the EV event loop in a separate thread, you can simply do this:

async { EV::run };

iconic. effortless. slay. 🧚


πŸŽ€ FUNCTIONS πŸŽ€

  • $revents = Coro::EV::timed_io_once $fileno_or_fh, $events[, $timeout] β€” blocks the coroutine until either the given event set has occurred on the fd, or the timeout has been reached (if timeout is missing or undef then there will be no timeout, so like... forever girl, be careful 🫢). Returns the received flags.

Consider using Coro::AnyEvent::readable and Coro::AnyEvent::writable instead β€” they work with any AnyEvent-supported event loop. πŸ’–

  • Coro::EV::timer_once $after β€” blocks the coroutine for at least $after seconds. ⏳

Consider using Coro::AnyEvent::sleep instead, which works with any AnyEvent-supported event loop. 🌸


πŸ‘‘ AUTHOR/SUPPORT/CONTACT πŸ‘‘

Marc A. Lehmann <schmorp@schmorp.de>
http://software.schmorp.de/pkg/Coro.html

and that's the whole page bestie!! πŸ’– this doc is from perl v5.40.1, dated 2025-03-21, so yeah β€” that's the era we're living in. go run your event loop in a coroutine and text me how it goes. mwah πŸ«Άβœ¨πŸ’…

πŸ“¦ where this page comes from