This call will block the current thread until the event loop has polled
for potential new events and instructs the event loop to poll for new
events once, without blocking.
Note that this call will not actually execute the poll, nor will it wait until there are some events, just block until the event loop has polled for new events, so other threads will have a chance to run.
This is useful when you have a thread that does some computations, but you still want to poll for new events from time to time. Simply call poll from time to time:
Although you should also consider idle or idle_upto in such cases.
|Coro::AnyEvent::sleep $seconds||This blocks the current thread for at least the given number of seconds.|
This call is similar to poll in that it will also poll for
events. Unlike poll, it will only resume the thread once there are no
events to handle anymore, i.e. when the process is otherwise idle.
This is good for background threads that shouldnt use CPU time when foreground jobs are ready to run.
Like idle, but with a maximum waiting time.
If your process is busy handling events, calling idle can mean that your thread will never be resumed. To avoid this, you can use idle_upto and specify a timeout, after which your thread will be resumed even if the process is completely busy.
|Coro::AnyEvent::readable $fh_or_fileno[, $timeout]|
|Coro::AnyEvent::writable $fh_or_fileno[, $timeout]||
Blocks the current thread until the given file handle (or file descriptor)
becomes readable (or writable), or the given timeout has elapsed,
whichever happens first. No timeout counts as infinite timeout.
Returns true when the file handle became ready, false when a timeout occured.
Note that these functions are quite inefficient as compared to using a single watcher (they recreate watchers on every invocation) or compared to using Coro::Handle.
Note also that they only work for sources that have reasonable non-blocking behaviour (e.g. not files).
Example: wait until STDIN becomes readable, then quit the program.
Unfortunately, few event loops (basically only EV and Event) support the kind of integration required for smooth operations well, and consequently, AnyEvent cannot completely offer the functionality required by this module, so we need to improvise.
Here is what this module does when it has to work with other event loops:
o run ready threads before blocking the process
This creates something similar to an idle watcher, i.e. a watcher that keeps the event loop from blocking but still polls for new events. (Unfortunately, some badly designed event loops (e.g. Event::Lib) dont support a timeout of 0 and will always block for a bit).
The callback for that timer will cede to other threads of the same or higher priority for as long as such threads exists. This has the effect of running all threads that have work to do until all threads block to wait for external events.
If no threads of equal or higher priority are ready, it will cede to any thread, but only once. This has the effect of running lower-priority threads as well, but it will not keep higher priority threads from receiving new events.
The priority used is simply the priority of the thread that runs the event loop, usually the main program, which usually has a priority of 0. Note that Coro::AnyEvent does not run an event loop for you, so unless the main program runs one, there will simply be no event loop to cede to (event handling will still work, somewhat inefficiently, but any thread will have a higher priority than event handling in that case).
o provide a suitable idle callback.
In addition to hooking into ready, this module will also provide a $Coro::idle handler that runs the event loop. It is best not to take advantage of this too often, as this is rather inefficient, but it should work perfectly fine.
o provide overrides for AnyEvents condvars
This module installs overrides for AnyEvents condvars. That is, when the module is loaded it will provide its own condition variables. This makes them coroutine-safe, i.e. you can safely block on them from within a coroutine.
o lead to data corruption or worse
As unblock_sub cannot be used by this module (as it is the module that implements it, basically), you must not call into the event loop recursively from any coroutine. This is not usually a difficult restriction to live with, just use condvars, unblock_sub or other means of inter-coroutine-communications.
If you use a module that supports AnyEvent (or uses the same event loop as AnyEvent, making it implicitly compatible), and it offers callbacks of any kind, then you must not block in them, either (or use e.g. unblock_sub), see the description of unblock_sub in the Coro module.
This also means that you should load the module as early as possible, as only condvars created after this module has been loaded will work correctly.
AnyEvent, to see which event loops are supported, Coro::EV and Coro::Event for more efficient and more correct solutions (they will be used automatically if applicable).
Marc A. Lehmann <firstname.lastname@example.org> http://software.schmorp.de/pkg/Coro.html
|perl v5.20.3||ANYEVENT (3)||2015-10-16|