Book/ev-Phpdoc专题
Ev
目录
- 简介
- 安装/配置
- 预定义常量
- 范例
- Watchers
- Watcher callbacks
- Periodic watcher operation modes
- Ev — The Ev class
- Ev::backend — Returns an integer describing the backend used by libev
- Ev::depth — Returns recursion depth
- Ev::embeddableBackends — Returns the set of backends that are embeddable in other event loops
- Ev::feedSignal — Feed a signal event info Ev
- Ev::feedSignalEvent — Feed signal event into the default loop
- Ev::iteration — Return the number of times the default event loop has polled for new events
- Ev::now — Returns the time when the last iteration of the default event loop has started
- Ev::nowUpdate — Establishes the current time by querying the kernel, updating the time returned by Ev::now in the progress
- Ev::recommendedBackends — Returns a bit mask of recommended backends for current platform
- Ev::resume — Resume previously suspended default event loop
- Ev::run — Begin checking for events and calling callbacks for the default loop
- Ev::sleep — Block the process for the given number of seconds
- Ev::stop — Stops the default event loop
- Ev::supportedBackends — Returns the set of backends supported by current libev configuration
- Ev::suspend — Suspend the default event loop
- Ev::time — Returns the current time in fractional seconds since the epoch
- Ev::verify — Performs internal consistency checks(for debugging)
- EvCheck — The EvCheck class
- EvCheck::__construct — Constructs the EvCheck watcher object
- EvCheck::createStopped — Create instance of a stopped EvCheck watcher
- EvChild — The EvChild class
- EvChild::__construct — Constructs the EvChild watcher object
- EvChild::createStopped — Create instance of a stopped EvCheck watcher
- EvChild::set — Configures the watcher
- EvEmbed — The EvEmbed class
- EvEmbed::__construct — Constructs the EvEmbed object
- EvEmbed::createStopped — Create stopped EvEmbed watcher object
- EvEmbed::set — Configures the watcher
- EvEmbed::sweep — Make a single, non-blocking sweep over the embedded loop
- EvFork — The EvFork class
- EvFork::__construct — Constructs the EvFork watcher object
- EvFork::createStopped — Creates a stopped instance of EvFork watcher class
- EvIdle — The EvIdle class
- EvIdle::__construct — Constructs the EvIdle watcher object
- EvIdle::createStopped — Creates instance of a stopped EvIdle watcher object
- EvIo — The EvIo class
- EvIo::__construct — Constructs EvIo watcher object
- EvIo::createStopped — Create stopped EvIo watcher object
- EvIo::set — Configures the watcher
- EvLoop — The EvLoop class
- EvLoop::backend — Returns an integer describing the backend used by libev
- EvLoop::check — Creates EvCheck object associated with the current event loop instance
- EvLoop::child — Creates EvChild object associated with the current event loop
- EvLoop::__construct — Constructs the event loop object
- EvLoop::defaultLoop — Returns or creates the default event loop
- EvLoop::embed — Creates an instance of EvEmbed watcher associated with the current EvLoop object
- EvLoop::fork — Creates EvFork watcher object associated with the current event loop instance
- EvLoop::idle — Creates EvIdle watcher object associated with the current event loop instance
- EvLoop::invokePending — Invoke all pending watchers while resetting their pending state
- EvLoop::io — Create EvIo watcher object associated with the current event loop instance
- EvLoop::loopFork — Must be called after a fork
- EvLoop::now — Returns the current "event loop time"
- EvLoop::nowUpdate — Establishes the current time by querying the kernel, updating the time returned by EvLoop::now in the progress
- EvLoop::periodic — Creates EvPeriodic watcher object associated with the current event loop instance
- EvLoop::prepare — Creates EvPrepare watcher object associated with the current event loop instance
- EvLoop::resume — Resume previously suspended default event loop
- EvLoop::run — Begin checking for events and calling callbacks for the loop
- EvLoop::signal — Creates EvSignal watcher object associated with the current event loop instance
- EvLoop::stat — Creates EvStat watcher object associated with the current event loop instance
- EvLoop::stop — Stops the event loop
- EvLoop::suspend — Suspend the loop
- EvLoop::timer — Creates EvTimer watcher object associated with the current event loop instance
- EvLoop::verify — Performs internal consistency checks(for debugging)
- EvPeriodic — The EvPeriodic class
- EvPeriodic::again — Simply stops and restarts the periodic watcher again
- EvPeriodic::at — Returns the absolute time that this watcher is supposed to trigger next
- EvPeriodic::__construct — Constructs EvPeriodic watcher object
- EvPeriodic::createStopped — Create a stopped EvPeriodic watcher
- EvPeriodic::set — Configures the watcher
- EvPrepare — The EvPrepare class
- EvPrepare::__construct — Constructs EvPrepare watcher object
- EvPrepare::createStopped — Creates a stopped instance of EvPrepare watcher
- EvSignal — The EvSignal class
- EvSignal::__construct — Constructs EvSignal watcher object
- EvSignal::createStopped — Create stopped EvSignal watcher object
- EvSignal::set — Configures the watcher
- EvStat — The EvStat class
- EvStat::attr — Returns the values most recently detected by Ev
- EvStat::__construct — Constructs EvStat watcher object
- EvStat::createStopped — Create a stopped EvStat watcher object
- EvStat::prev — Returns the previous set of values returned by EvStat::attr
- EvStat::set — Configures the watcher
- EvStat::stat — Initiates the stat call
- EvTimer — The EvTimer class
- EvTimer::again — Restarts the timer watcher
- EvTimer::__construct — Constructs an EvTimer watcher object
- EvTimer::createStopped — Creates EvTimer stopped watcher object
- EvTimer::set — Configures the watcher
- EvWatcher — The EvWatcher class
- EvWatcher::clear — Clear watcher pending status
- EvWatcher::__construct — Abstract constructor of a watcher object
- EvWatcher::feed — Feeds the given revents set into the event loop
- EvWatcher::getLoop — Returns the loop responsible for the watcher
- EvWatcher::invoke — Invokes the watcher callback with the given received events bit mask
- EvWatcher::keepalive — Configures whether to keep the loop from returning
- EvWatcher::setCallback — Sets new callback for the watcher
- EvWatcher::start — Starts the watcher
- EvWatcher::stop — Stops the watcher
简介
Ev is a static class providing access to the default loop and to some common operations.
类摘要
Ev
final class Ev {
/* Constants */
const int
Ev::FLAG_AUTO = 0 ;
const int
Ev::FLAG_NOENV = 16777216 ;
const int
Ev::FLAG_FORKCHECK = 33554432 ;
const int
Ev::FLAG_NOINOTIFY = 1048576 ;
const int
Ev::FLAG_SIGNALFD = 2097152 ;
const int
Ev::FLAG_NOSIGMASK = 4194304 ;
const int
Ev::RUN_NOWAIT = 1 ;
const int
Ev::RUN_ONCE = 2 ;
const int
Ev::BREAK_CANCEL = 0 ;
const int
Ev::BREAK_ONE = 1 ;
const int
Ev::BREAK_ALL = 2 ;
const int
Ev::MINPRI = -2 ;
const int
Ev::MAXPRI = 2 ;
const int
Ev::READ = 1 ;
const int
Ev::WRITE = 2 ;
const int
Ev::TIMER = 256 ;
const int
Ev::PERIODIC = 512 ;
const int
Ev::SIGNAL = 1024 ;
const int
Ev::CHILD = 2048 ;
const int
Ev::STAT = 4096 ;
const int
Ev::IDLE = 8192 ;
const int
Ev::PREPARE = 16384 ;
const int
Ev::CHECK = 32768 ;
const int
Ev::EMBED = 65536 ;
const int
Ev::CUSTOM = 16777216 ;
const int
Ev::ERROR = 2147483648 ;
const int
Ev::BACKEND_SELECT = 1 ;
const int
Ev::BACKEND_POLL = 2 ;
const int
Ev::BACKEND_EPOLL = 4 ;
const int
Ev::BACKEND_KQUEUE = 8 ;
const int
Ev::BACKEND_DEVPOLL = 16 ;
const int
Ev::BACKEND_PORT = 32 ;
const int
Ev::BACKEND_ALL = 63 ;
const int
Ev::BACKEND_MASK = 65535 ;
/* 方法 */
final public static int <span class="methodname">backend ( <span class="methodparam">void )
final public static int <span class="methodname">depth ( void )
final public static int <span class="methodname">embeddableBackends ( <span class="methodparam">void )
final public
static void
feedSignal (
int $signum )
final public
static void
feedSignalEvent ( <span
class="methodparam"> int $signum )
final public static int <span class="methodname">iteration ( <span class="methodparam">void )
final public static float now ( <span class="methodparam">void )
final public static void nowUpdate ( <span class="methodparam">void )
final public static int <span class="methodname">recommendedBackends ( <span class="methodparam">void )
final public static void resume ( <span class="methodparam">void )
final public
static void
run ([ <span
class="type">int $flags ] )
final public
static void
sleep ( <span
class="type">float $seconds )
final public
static void
stop ([
int $how ] )
final public static int <span class="methodname">supportedBackends ( <span class="methodparam">void )
final public static void suspend ( <span class="methodparam">void )
final public static float time ( <span class="methodparam">void )
final public static void verify ( <span class="methodparam">void )
}
预定义常量
Flags passed to create a loop:
Ev::FLAG_AUTO
The default flags value
Ev::FLAG_NOENV
If this flag used(or the program runs setuid or setgid), libev won't
look at the environment variable LIBEV_FLAGS . Otherwise(by default),
LIBEV_FLAGS will override the flags completely if it is found. Useful
for performance tests and searching for bugs.
Ev::FLAG_FORKCHECK
Makes libev check for a fork in each iteration, instead of calling <span
class="methodname">EvLoop::fork manually. This works by calling
getpid() on every iteration of the loop, and thus this might slow down
the event loop with lots of loop iterations, but usually is not
noticeable. This flag setting cannot be overridden or specified in the
LIBEV_FLAGS environment variable.
Ev::FLAG_NOINOTIFY
When this flag is specified, libev won't attempt to use the inotify
API for its
» ev_stat
watchers. The flag can be useful to conserve inotify file descriptors,
as otherwise each loop using ev_stat watchers consumes one inotify
handle.
Ev::FLAG_SIGNALFD
When this flag is specified, libev will attempt to use the signalfd
API for its
» ev_signal
(and
» ev_child
) watchers. This API delivers signals synchronously, which makes it both
faster and might make it possible to get the queued signal data. It can
also simplify signal handling with threads, as long as signals are
properly blocked in threads. Signalfd will not be used by default.
Ev::FLAG_NOSIGMASK
When this flag is specified, libev will avoid to modify the signal
mask. Specifically, this means having to make sure signals are unblocked
before receiving them.
This behaviour is useful for custom signal handling, or handling signals only in specific threads.
Flags passed to Ev::run , or <span class="methodname">EvLoop::run
Ev::RUN_NOWAIT
Means that event loop will look for new events, will handle those events
and any already outstanding ones, but will not wait and block the
process in case there are no events and will return after one iteration
of the loop. This is sometimes useful to poll and handle new events
while doing lengthy calculations, to keep the program responsive.
Ev::RUN_ONCE
Means that event loop will look for new events (waiting if necessary)
and will handle those and any already outstanding ones. It will block
the process until at least one new event arrives (which could be an
event internal to libev itself, so there is no guarantee that a
user-registered callback will be called), and will return after one
iteration of the loop.
Flags passed to Ev::stop , or <span class="methodname">EvLoop::stop
Ev::BREAK_CANCEL
Cancel the break operation.
Ev::BREAK_ONE
Makes the innermost Ev::run (or <span
class="methodname">EvLoop::run ) call return.
Ev::BREAK_ALL
Makes all nested Ev::run (or <span
class="methodname">EvLoop::run ) calls return.
Watcher priorities:
Ev::MINPRI
Minimum allowed watcher priority.
Ev::MAXPRI
Maximum allowed watcher priority.
Bit masks of (received) events:
Ev::READ
The file descriptor in the EvIo watcher
has become readable.
Ev::WRITE
The file descriptor in the EvIo watcher
has become writable.
Ev::TIMER
EvTimer watcher has been timed out.
Ev::PERIODIC
EvPeriodic watcher has been timed out.
Ev::SIGNAL
A signal specified in <span
class="methodname">EvSignal::__construct has been received.
Ev::CHILD
The pid specified in <span
class="methodname">EvChild::__construct has received a status
change.
Ev::STAT
The path specified in EvStat watcher
changed its attributes.
Ev::IDLE
EvIdle watcher works when there is
nothing to do with other watchers.
Ev::PREPARE
All EvPrepare watchers are invoked just
before Ev::run starts. Thus, <span
class="classname">EvPrepare watchers are the last watchers
invoked before the event loop sleeps or polls for new events.
Ev::CHECK
All EvCheck watchers are queued just
after Ev::run has gathered the new
events, but before it queues any callbacks for any received events.
Thus, EvCheck watchers will be invoked
before any other watchers of the same or lower priority within an event
loop iteration.
Ev::EMBED
The embedded event loop specified in the <span
class="classname">EvEmbed watcher needs attention.
Ev::CUSTOM
Not ever sent(or otherwise used) by libev itself, but can be freely
used by libev users to signal watchers (e.g. via <span
class="methodname">EvWatcher::feed ).
Ev::ERROR
An unspecified error has occurred, the watcher has been stopped. This
might happen because the watcher could not be properly started because
libev ran out of memory, a file descriptor was found to be closed or
any other problem. Libev considers these application bugs. See also
» ANATOMY OF A WATCHER
Backend flags:
Ev::BACKEND_SELECT
select(2) backend
Ev::BACKEND_POLL
poll(2) backend
Ev::BACKEND_EPOLL
Linux-specific epoll(7) backend for both pre- and post-2.6.9 kernels
Ev::BACKEND_KQUEUE
kqueue backend used on most BSD systems. <span
class="classname">EvEmbed watcher could be used to embed one
loop(with kqueue backend) into another. For instance, one can try to
create an event loop with kqueue backend and use it for sockets only.
Ev::BACKEND_DEVPOLL
Solaris 8 backend. This is not implemented yet.
Ev::BACKEND_PORT
Solaris 10 event port mechanism with a good scaling.
Ev::BACKEND_ALL
Try all backends(even currupted ones). It's not recommended to use it
explicitly. Bitwise operators should be applied here(e.g.
Ev::BACKEND_ALL & ~ Ev::BACKEND_KQUEUE ) Use <span
class="methodname">Ev::recommendedBackends , or don't specify any
backends at all.
Ev::BACKEND_MASK
Not a backend, but a mask to select all backend bits from flags value
to mask out any backends(e.g. when modifying the LIBEV_FLAGS
environment variable).
Note:
For the default loop during module initialization phase Ev registers » ev_loop_fork call by means of pthread_atfork (if available).
Note:
There are methods providing access to the default event loop in Ev class(e.g. <span class="methodname">Ev::iteration , <span class="methodname">Ev::depth etc.) For custom loops (created with EvLoop::__construct ) these values may be accessed via corresponding properties and methods of the EvLoop class.
The instance of the default event loop itself can be fetched by means of EvLoop::defaultLoop method.
Ev::backend
Returns an integer describing the backend used by libev
说明
final public static int <span class="methodname">Ev::backend ( <span class="methodparam">void )
Returns an integer describing the backend used by libev . See Backend flags
参数
此函数没有参数。
返回值
Returns an integer(bit mask) describing the backend used by libev .
参见
- EvEmbed
- Ev::embeddableBackends
- Ev::recommendedBackends
- Ev::supportedBackends
- Backend flags
Ev::depth
Returns recursion depth
说明
final public static int <span class="methodname">Ev::depth ( <span class="methodparam">void )
The number of times Ev::run was entered
minus the number of times Ev::run was
exited normally, in other words, the recursion depth. Outside <span
class="methodname">Ev::run , this number is 0 . In a
callback, this number is 1 , unless <span
class="methodname">Ev::run was invoked recursively (or from
another thread), in which case it is higher.
参数
此函数没有参数。
返回值
ev_depth returns recursion depth of the default loop.
参见
- Ev::iteration
Ev::embeddableBackends
Returns the set of backends that are embeddable in other event loops
说明
final public static int <span class="methodname">Ev::embeddableBackends ( <span class="methodparam">void )
Returns the set of backends that are embeddable in other event loops.
参数
此函数没有参数。
返回值
Returns a bit mask which can containing backend flags combined using bitwise OR operator.
范例
示例 #1 Embedding loop created with kqueue backend into the default loop
<?php
/*
* Check if kqueue is available but not recommended and create a kqueue backend
* for use with sockets (which usually work with any kqueue implementation).
* Store the kqueue/socket-only event loop in loop_socket. (One might optionally
* use EVFLAG_NOENV, too)
*
* Example borrowed from
* http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
*/
$loop = EvLoop::defaultLoop();
$socket_loop = NULL;
$embed = NULL;
if (Ev::supportedBackends() & ~Ev::recommendedBackends() & Ev::BACKEND_KQUEUE) {
if (($socket_loop = new EvLoop(Ev::BACKEND_KQUEUE))) {
$embed = new EvEmbed($loop);
}
}
if (!$socket_loop) {
$socket_loop = $loop;
}
// Now use $socket_loop for all sockets, and $loop for anything else
?>
参见
- EvEmbed
- Ev::recommendedBackends
- Ev::supportedBackends
- Backend flags
- Examples
Ev::feedSignal
Feed a signal event info Ev
说明
final public
static void
Ev::feedSignal ( <span
class="methodparam"> int $signum )
Simulates a signal receive. It is safe to call this function at any time, from any context, including signal handlers or random threads. Its main use is to customise signal handling in the process.
Unlike Ev::feedSignalEvent , this works regardless of which loop has registered the signal.
参数
signum
Signal number. See signal(7) man page for detals. You can use
constants exported by pcntl extension.
返回值
没有返回值。
参见
- Ev::feedSignalEvent
Ev::feedSignalEvent
Feed signal event into the default loop
说明
final public
static void
Ev::feedSignalEvent ( <span
class="methodparam"> int $signum )
Feed signal event into the default loop. Ev will react to this call as
if the signal specified by signal had occurred.
参数
signum
Signal number. See signal(7) man page for detals. See also constants
exported by pcntl extension.
返回值
没有返回值。
参见
- Ev::feedSignal
Ev::iteration
Return the number of times the default event loop has polled for new events
说明
final public static int <span class="methodname">Ev::iteration ( <span class="methodparam">void )
Return the number of times the event loop has polled for new events. Sometimes useful as a generation counter.
参数
此函数没有参数。
返回值
Returns number of polls of the default event loop.
参见
- Ev::depth
Ev::now
Returns the time when the last iteration of the default event loop has started
说明
final public static float Ev::now ( <span class="methodparam">void )
Returns the time when the last iteration of the default event loop has started. This is the time that timers( <span class="classname">EvTimer and <span class="classname">EvPeriodic ) are based on, and referring to it is usually faster then calling Ev::time .
参数
此函数没有参数。
返回值
Returns number of seconds(fractional) representing the time when the last iteration of the default event loop has started.
参见
- Ev::nowUpdate
Ev::nowUpdate
Establishes the current time by querying the kernel, updating the time returned by Ev::now in the progress
说明
final public static void Ev::nowUpdate ( <span class="methodparam">void )
Establishes the current time by querying the kernel, updating the time returned by Ev::now in the progress. This is a costly operation and is usually done automatically within Ev::run .
This method is rarely useful, but when some event callback runs for a very long time without entering the event loop, updating libev 's consideration of the current time is a good idea.
参数
此函数没有参数。
返回值
没有返回值。
参见
- Ev::now
Ev::recommendedBackends
Returns a bit mask of recommended backends for current platform
说明
final public static int <span class="methodname">Ev::recommendedBackends ( <span class="methodparam">void )
Returns the set of all backends compiled into this binary of libev and also recommended for this platform, meaning it will work for most file descriptor types. This set is often smaller than the one returned by ev_supported_backends , as for example kqueue is broken on most BSD systems and will not be auto-detected unless it is requested explicitly. This is the set of backends that libev will probe no backends specified explicitly.
参数
此函数没有参数。
返回值
Returns a bit mask which can containing backend flags combined using bitwise OR operator.
范例
示例 #1 Embedding one loop into another
<?php
/*
* Try to get an embeddable event loop and embed it into the default event loop.
* If it is impossible, use the default
* loop. The default loop is stored in $loop_hi, while the embeddable loop is
* stored in $loop_lo(which is $loop_hi in the case no embeddable loop can be
* used).
*
* Sample translated to PHP
* http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
*/
$loop_hi = EvLoop::defaultLoop();
$loop_lo = NULL;
$embed = NULL;
/*
* See if there is a chance of getting one that works
* (flags' value of 0 means autodetection)
*/
$loop_lo = Ev::embeddableBackends() & Ev::recommendedBackends()
? new EvLoop(Ev::embeddableBackends() & Ev::recommendedBackends())
: 0;
if ($loop_lo) {
$embed = new EvEmbed($loop_lo, function () {});
} else {
$loop_lo = $loop_hi;
}
?>
参见
- EvEmbed
- Ev::embeddableBackends
- Ev::supportedBackends
- Backend flags
- Examples
Ev::resume
Resume previously suspended default event loop
说明
final public static void Ev::resume ( <span class="methodparam">void )
Ev::suspend and <span class="methodname">Ev::resume methods suspend and resume a loop correspondingly.
All timer watchers will be delayed by the time spend between suspend and resume , and all periodic watchers will be rescheduled(that is, they will lose any events that would have occurred while suspended).
After calling Ev::suspend it is not allowed to call any function on the given loop other than <span class="methodname">Ev::resume . Also it is not allowed to call Ev::resume without a previous call to Ev::suspend .
Calling suspend / resume has the side effect of updating the event loop time(see Ev::nowUpdate ).
参数
此函数没有参数。
返回值
没有返回值。
参见
- Ev::suspend
Ev::run
Begin checking for events and calling callbacks for the default loop
说明
final public
static void
Ev::run ([
int $flags ] )
Begin checking for events and calling callbacks for the default loop .
Returns when a callback calls Ev::stop
method, or the flags are nonzero(in which case the return value is true)
or when there are no active watchers which reference the loop( <span
class="methodname">EvWatcher::keepalive is true), in which
case the return value will be false. The return value can
generally be interpreted as if true, there is more work left to
do .
参数
flags
Optional parameter flags can be one of the following:
flags |
Description |
|---|---|
0 |
The default behavior described above |
Ev::RUN_ONCE |
Block at most one(wait, but don't loop) |
Ev::RUN_NOWAIT |
Don't block at all(fetch/handle events, but don't wait) |
See the run flag constants .
返回值
没有返回值。
参见
- Ev::stop
- EvLoop::run
Ev::sleep
Block the process for the given number of seconds
说明
final public
static void
Ev::sleep (
float $seconds )
Block the process for the given number of seconds.
参数
seconds
Fractional number of seconds
返回值
没有返回值。
Ev::stop
Stops the default event loop
说明
final public
static void
Ev::stop ([
int $how ] )
Stops the default event loop
参数
how
One of Ev::BREAK_*
constants .
返回值
没有返回值。
参见
- Ev::run
Ev::supportedBackends
Returns the set of backends supported by current libev configuration
说明
final public static int <span class="methodname">Ev::supportedBackends ( <span class="methodparam">void )
Returns the set of backends supported by current libev configuration.
参数
此函数没有参数。
返回值
Returns a bit mask which can containing backend flags combined using bitwise OR operator.
范例
示例 #1 Embedding loop created with kqueue backend into the default loop
<?php
/*
* Check if kqueue is available but not recommended and create a kqueue backend
* for use with sockets (which usually work with any kqueue implementation).
* Store the kqueue/socket-only event loop in loop_socket. (One might optionally
* use EVFLAG_NOENV, too)
*
* Example borrowed from
* http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
*/
$loop = EvLoop::defaultLoop();
$socket_loop = NULL;
$embed = NULL;
if (Ev::supportedBackends() & ~Ev::recommendedBackends() & Ev::BACKEND_KQUEUE) {
if (($socket_loop = new EvLoop(Ev::BACKEND_KQUEUE))) {
$embed = new EvEmbed($loop);
}
}
if (!$socket_loop) {
$socket_loop = $loop;
}
// Now use $socket_loop for all sockets, and $loop for anything else
?>
参见
- EvEmbed
- Ev::recommendedBackends
- Ev::embeddableBackends
- Backend flags
- Examples
Ev::suspend
Suspend the default event loop
说明
final public static void Ev::suspend ( <span class="methodparam">void )
Ev::suspend and <span class="methodname">Ev::resume methods suspend and resume the default loop correspondingly.
All timer watchers will be delayed by the time spend between suspend and resume , and all periodic watchers will be rescheduled(that is, they will lose any events that would have occurred while suspended).
After calling Ev::suspend it is not allowed to call any function on the given loop other than <span class="methodname">Ev::resume . Also it is not allowed to call Ev::resume without a previous call to Ev::suspend .
参数
此函数没有参数。
返回值
没有返回值。
参见
- Ev::resume
Ev::time
Returns the current time in fractional seconds since the epoch
说明
final public static float Ev::time ( <span class="methodparam">void )
Returns the current time in fractional seconds since the epoch. Consider using Ev::now
参数
此函数没有参数。
返回值
Returns the current time in fractional seconds since the epoch.
参见
- Ev::now
Ev::verify
Performs internal consistency checks(for debugging)
说明
final public static void Ev::verify ( <span class="methodparam">void )
Performs internal consistency checks(for debugging libev ) and abort the program if any data structures were found to be corrupted.
参数
此函数没有参数。
返回值
没有返回值。
简介
EvPrepare and <span class="classname">EvCheck watchers are usually used in pairs. EvPrepare watchers get invoked before the process blocks, EvCheck afterwards.
It is not allowed to call EvLoop::run or similar methods or functions that enter the current event loop from either EvPrepare or <span class="classname">EvCheck watchers. Other loops than the current one are fine, however. The rationale behind this is that one don't need to check for recursion in those watchers, i.e. the sequence will always be: EvPrepare -> blocking -> <span class="classname">EvCheck , so having a watcher of each kind they will always be called in pairs bracketing the blocking call.
The main purpose is to integrate other event mechanisms into libev and their use is somewhat advanced. They could be used, for example, to track variable changes, implement custom watchers, integrate net-snmp or a coroutine library and lots more. They are also occasionally useful to cache some data and want to flush it before blocking.
It is recommended to give EvCheck
watchers highest( Ev::MAXPRI ) priority, to ensure that they are
being run before any other watchers after the poll (this doesn’t matter
for EvPrepare watchers).
Also, EvCheck watchers should not activate/feed events. While libev fully supports this, they might get executed before other EvCheck watchers did their job.
类摘要
EvCheck
class EvCheck extends EvWatcher {
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public <span
class="methodname">__construct (
callable $callback [, <span
class="methodparam"> mixed $data [,
int $priority
]] )
final public
static object
createStopped ( <span
class="methodparam"> string $callback
[, string
$data [, <span
class="type">string $priority ]] )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
EvCheck::__construct
Constructs the EvCheck watcher object
说明
public <span
class="methodname">EvCheck::__construct ( <span
class="methodparam"> callable $callback
[, mixed
$data [, <span
class="type">int $priority ]] )
Constructs the EvCheck watcher object.
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvCheck object on success.
参见
- EvPrepare
- EvLoop::check
EvCheck::createStopped
Create instance of a stopped EvCheck watcher
说明
final public
static object
EvCheck::createStopped ( <span
class="methodparam"> string $callback
[, string
$data [, <span
class="type">string $priority ]] )
Create instance of a stopped EvCheck watcher
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvCheck object on success.
参见
- EvPrepare
简介
EvChild watchers trigger when the process
receives a SIGCHLD in response to some child status changes (most
typically when a child dies or exits). It is permissible to install an
EvChild watcher after the child has been forked(which implies it
might have already exited), as long as the event loop isn't entered(or
is continued from a watcher), i.e. forking and then immediately
registering a watcher for the child is fine, but forking and registering
a watcher a few event loop iterations later or in the next callback
invocation is not.
It is allowed to register EvChild watchers in the default loop only.
类摘要
EvChild
class EvChild extends EvWatcher {
/* 属性 */
public $pid ;
public $rpid ;
public $rstatus ;
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public <span
class="methodname">__construct (
int $pid , <span
class="methodparam"> bool $trace ,
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
final public
static object
createStopped ( <span
class="methodparam"> int $pid ,
bool $trace
, callable
$callback [, <span
class="type">mixed $data [,
int $priority ]] )
public void
set ( <span
class="type">int $pid ,
bool $trace )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
属性
pid
Readonly . The process ID this watcher watches out for, or 0 ,
meaning any process ID.
rpid
Readonly .The process ID that detected a status change.
rstatus
Readonly . The process exit status caused by rpid .
EvChild::__construct
Constructs the EvChild watcher object
说明
public <span
class="methodname">EvChild::__construct ( <span
class="methodparam"> int $pid ,
bool $trace
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Constructs the EvChild watcher object.
Call the callback when a status change for process ID pid (or any
PID if pid is 0 ) has been received(a status change happens
when the process terminates or is killed, or, when trace is
true, additionally when it is stopped or continued). In other
words, when the process receives a SIGCHLD , Ev will fetch the
outstanding exit/wait status for all changed/zombie children and call
the callback.
It is valid to install a child watcher after an <span class="classname">EvChild has exited but before the event loop has started its next iteration. For example, first one calls fork , then the new child process might exit, and only then an <span class="classname">EvChild watcher is installed in the parent for the new PID .
You can access both exit/tracing status and pid by using the rstatus
and rpid properties of the watcher object.
The number of PID watchers per PID is unlimited. All of them will be called.
The EvChild::createStopped method doesn't start(activate) the newly created watcher.
参数
pid
Wait for status changes of process PID(or any process if PID is
specified as 0 ).
trace
If false, only activate the watcher when the process terminates.
Otherwise(true) additionally activate the watcher when the process
is stopped or continued.
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvChild object on success.
参见
- EvLoop::child
EvChild::createStopped
Create instance of a stopped EvCheck watcher
说明
final public
static object
EvChild::createStopped ( <span
class="methodparam"> int $pid ,
bool $trace
, callable
$callback [, <span
class="type">mixed $data [,
int $priority ]] )
The same as EvChild::__construct , but doesn't start the watcher automatically.
参数
pid
The same as for EvChild::__construct
trace
The same as for EvChild::__construct
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
参见
- EvChild::__construct
- EvLoop::child
EvChild::set
Configures the watcher
说明
public void
EvChild::set ( <span
class="methodparam"> int $pid ,
bool $trace
)
参数
pid
The same as for EvChild::__construct
trace
The same as for EvChild::__construct
返回值
没有返回值。
参见
- EvChild::__construct
简介
Used to embed one event loop into another.
类摘要
EvEmbed
class EvEmbed extends EvWatcher {
/* 属性 */
public $embed ;
/* 方法 */
public <span
class="methodname">__construct (
object $other [, <span
class="methodparam"> callable $callback
[, mixed
$data [, <span
class="type">int $priority ]]] )
final public
static void
createStopped ( <span
class="methodparam"> object $other
[, callable
$callback [, <span
class="type">mixed $data [,
int $priority ]]] )
public void
set ( <span
class="type">object $other )
public void sweep ( <span class="methodparam">void )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
属性
is_active
data
is_pending
priority
embed
EvEmbed::__construct
Constructs the EvEmbed object
说明
public <span
class="methodname">EvEmbed::__construct ( <span
class="methodparam"> object $other
[, callable
$callback [, <span
class="type">mixed $data [,
int $priority ]]] )
This is a rather advanced watcher type that lets to embed one event loop into another(currently only IO events are supported in the embedded loop, other types of watchers might be handled in a delayed or incorrect fashion and must not be used).
See » the libev documentation for details.
This watcher is most useful on BSD systems without working kqueue to still be able to handle a large number of sockets. See example below.
参数
other
Instance of EvLoop . The loop to embed,
this loop must be embeddable(see <span
class="methodname">Ev::embeddableBackends ).
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvEmbed object on success.
范例
示例 #1 Embedding loop created with kqueue backend into the default loop
<?php
/*
* Check if kqueue is available but not recommended and create a kqueue backend
* for use with sockets (which usually work with any kqueue implementation).
* Store the kqueue/socket-only event loop in loop_socket. (One might optionally
* use EVFLAG_NOENV, too)
*
* Example borrowed from
* http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
*/
$loop = EvLoop::defaultLoop();
$socket_loop = NULL;
$embed = NULL;
if (Ev::supportedBackends() & ~Ev::recommendedBackends() & Ev::BACKEND_KQUEUE) {
if (($socket_loop = new EvLoop(Ev::BACKEND_KQUEUE))) {
$embed = new EvEmbed($loop);
}
}
if (!$socket_loop) {
$socket_loop = $loop;
}
// Now use $socket_loop for all sockets, and $loop for anything else
?>
参见
- Ev::embeddableBackends
EvEmbed::createStopped
Create stopped EvEmbed watcher object
说明
final public
static void
EvEmbed::createStopped ( <span
class="methodparam"> object $other
[, callable
$callback [, <span
class="type">mixed $data [,
int $priority ]]] )
The same as EvEmbed::__construct , but doesn't start the watcher automatically.
参数
other
The same as for EvEmbed::__construct
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns stopped EvEmbed object on success.
参见
- EvEmbed::__construct
- Ev::embeddableBackends
EvEmbed::set
Configures the watcher
说明
public void
EvEmbed::set ( <span
class="methodparam"> object $other )
Configures the watcher to use other event loop object.
参数
other
The same as for EvEmbed::__construct
返回值
没有返回值。
EvEmbed::sweep
Make a single, non-blocking sweep over the embedded loop
说明
public void EvEmbed::sweep ( <span class="methodparam">void )
Make a single, non-blocking sweep over the embedded loop. Works similarly to the following, but in the most appropriate way for embedded loops:
<?php
$other->start(Ev::RUN_NOWAIT);
?>
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvWatcher::start
简介
Fork watchers are called when a fork() was detected (usually because whoever signalled libev about it by calling <span class="methodname">EvLoop::fork ). The invocation is done before the event loop blocks next and before <span class="classname">EvCheck watchers are being called, and only in the child after the fork. Note, that if whoever calling <span class="methodname">EvLoop::fork calls it in the wrong process, the fork handlers will be invoked, too.
类摘要
EvFork
class EvFork extends EvWatcher {
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public <span
class="methodname">__construct (
callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
final public
static object
createStopped ( <span
class="methodparam"> string $callback
[, string
$data [, <span
class="type">string $priority ]] )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
EvFork::__construct
Constructs the EvFork watcher object
说明
public <span
class="methodname">EvFork::__construct ( <span
class="methodparam"> callable $callback
[, mixed
$data = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Constructs the EvFork watcher object and starts the watcher automatically.
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvFork object on success.
参见
- EvLoop::fork
- EvCheck
EvFork::createStopped
Creates a stopped instance of EvFork watcher class
说明
final public
static object
EvFork::createStopped ( <span
class="methodparam"> string $callback
[, string
$data [, <span
class="type">string $priority ]] )
The same as EvFork::__construct , but doesn't start the watcher automatically.
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvFork(stopped) object on success.
参见
- EvFork::__construct
简介
EvIdle watchers trigger events when no other events of the same or higher priority are pending ( <span class="classname">EvPrepare , <span class="classname">EvCheck and other <span class="classname">EvIdle watchers do not count as receiving events ).
Thus, as long as the process is busy handling sockets or timeouts(or even signals) of the same or higher priority it will not be triggered. But when the process is in idle(or only lower-priority watchers are pending), the EvIdle watchers are being called once per event loop iteration - until stopped, that is, or the process receives more events and becomes busy again with higher priority stuff.
Apart from keeping the process non-blocking(which is a useful on its own sometimes), EvIdle watchers are a good place to do "pseudo-background processing" , or delay processing stuff to after the event loop has handled all outstanding events.
The most noticeable effect is that as long as any idle watchers are active, the process will not block when waiting for new events.
类摘要
EvIdle
class EvIdle extends EvWatcher {
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public <span
class="methodname">__construct (
callable $callback [, <span
class="methodparam"> mixed $data [,
int $priority
]] )
final public
static object
createStopped ( <span
class="methodparam"> string $callback
[, mixed
$data [, <span
class="type">int $priority ]] )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
EvIdle::__construct
Constructs the EvIdle watcher object
说明
public <span
class="methodname">EvIdle::__construct ( <span
class="methodparam"> callable $callback
[, mixed
$data [, <span
class="type">int $priority ]] )
Constructs the EvIdle watcher object and starts the watcher automatically.
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvIdle object on success.
参见
- EvIdle::createStopped
- EvLoop::idle
EvIdle::createStopped
Creates instance of a stopped EvIdle watcher object
说明
final public
static object
EvIdle::createStopped ( <span
class="methodparam"> string $callback
[, mixed
$data [, <span
class="type">int $priority ]] )
The same as EvIdle::__construct , but doesn't start the watcher automatically.
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvIdle object on success.
参见
- EvIdle::__construct
- EvLoop::idle
简介
EvIo watchers check whether a file descriptor(or socket, or a stream castable to numeric file descriptor) is readable or writable in each iteration of the event loop, or, more precisely, when reading would not block the process and writing would at least be able to write some data. This behaviour is called level-triggering because events are kept receiving as long as the condition persists. To stop receiving events just stop the watcher.
The number of read and/or write event watchers per fd is unlimited.
Setting all file descriptors to non-blocking mode is also usually a good
idea(but not required).
Another thing to watch out for is that it is quite easy to receive false
readiness notifications, i.e. the callback might be called with
Ev::READ but a subsequent read() will actually block because
there is no data. It is very easy to get into this situation. Thus it is
best to always use non-blocking I/O: An extra read() returning
EAGAIN (or similar) is far preferable to a program hanging until
some data arrives.
If for some reason it is impossible to run the fd in non-blocking
mode, then separately re-test whether a file descriptor is really ready.
Some people additionally use SIGALRM and an interval timer, just
to be sure thry won't block infinitely.
Always consider using non-blocking mode.
类摘要
EvIo
class EvIo extends EvWatcher {
/* 属性 */
public $fd ;
public $events ;
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public <span
class="methodname">__construct (
mixed $fd , <span
class="methodparam"> int $events ,
callable
$callback [, <span
class="type">mixed $data [,
int $priority ]] )
final public
static EvIo
createStopped ( <span
class="methodparam"> mixed $fd ,
int $events
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
public void
set ( <span
class="type">mixed $fd ,
int $events )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
属性
fd
events
EvIo::__construct
Constructs EvIo watcher object
说明
public <span
class="methodname">EvIo::__construct ( <span
class="methodparam"> mixed $fd ,
int $events
, callable
$callback [, <span
class="type">mixed $data [,
int $priority ]] )
Constructs EvIo watcher object and starts the watcher automatically.
参数
fd
Can be a stream opened with fopen or
similar functions, numeric file descriptor, or socket.
events
Ev::READ and/or Ev::WRITE . See
the bit masks .
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvIo object on success.
参见
- EvIo::createStopped
- EvLoop::io
EvIo::createStopped
Create stopped EvIo watcher object
说明
final public
static EvIo
EvIo::createStopped ( <span
class="methodparam"> mixed $fd ,
int $events
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
The same as EvIo::__construct , but doesn't start the watcher automatically.
参数
fd
The same as for EvIo::__construct
events
The same as for EvIo::__construct
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvIo object on success.
参见
- EvIo::__construct
- EvLoop::io
EvIo::set
Configures the watcher
说明
public void
EvIo::set (
mixed $fd , <span
class="methodparam"> int $events )
Configures the EvIo watcher
参数
fd
The same as for EvIo::__construct
events
The same as for EvIo::__construct
返回值
没有返回值。
简介
Represents an event loop that is always distinct from the default loop . Unlike the default loop , it cannot handle <span class="classname">EvChild watchers.
Having threads we have to create a loop per thread, and use the default loop in the parent thread.
The default event loop is initialized automatically by Ev . It is accessible via methods of the Ev class, or via EvLoop::defaultLoop method.
类摘要
EvLoop
final class EvLoop {
/* 属性 */
public $data ;
public $backend ;
public $is_default_loop ;
public $iteration ;
public $pending ;
public $io_interval ;
public $timeout_interval ;
public $depth ;
/* 方法 */
public int <span class="methodname">backend ( <span class="methodparam">void )
final public
EvCheck check
( string
$callback [, <span
class="type">string $data [, <span
class="methodparam"> string $priority
]] )
final public
EvChild child
( string $pid
, string
$trace , <span
class="type">string $callback [, <span
class="methodparam"> string $data
[, string
$priority ]] )
public <span
class="methodname">__construct ([
int $flags [, <span
class="methodparam"> mixed $data <span
class="initializer"> = NULL [, <span
class="methodparam"> float $io_interval
= 0.0 [, <span
class="methodparam"> float $timeout_interval
= 0.0 ]]]] )
public <span
class="modifier">static EvLoop <span
class="methodname">defaultLoop ([
int $flags =
Ev::FLAG_AUTO [, <span
class="type">mixed $data =
NULL [, <span
class="type">float $io_interval =
0. [, <span
class="type">float $timeout_interval
= 0. ]]]] )
final public
EvEmbed embed
( string $other
[, string
$callback [, <span
class="type">string $data [, <span
class="methodparam"> string $priority
]]] )
final public
EvFork fork (
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
final public
EvIdle idle (
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
public void invokePending ( <span class="methodparam">void )
final public
EvIo io (
mixed $fd
, int $events
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
public void loopFork ( <span class="methodparam">void )
public float now ( <span class="methodparam">void )
public void nowUpdate ( <span class="methodparam">void )
final public
EvPeriodic <span
class="methodname">periodic ( <span
class="type">float $offset ,
float $interval , <span
class="methodparam"> callable $callback
[, mixed
$data = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
final public
EvPrepare <span
class="methodname">prepare ( <span
class="type">callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
public void resume ( <span class="methodparam">void )
public void
run ([ <span
class="type">int $flags = 0
] )
final public
EvSignal <span
class="methodname">signal ( <span
class="type">int $signum ,
callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
final public
EvStat stat (
string $path
, float
$interval , <span
class="type">callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
public void
stop ([
int $how ] )
public void suspend ( <span class="methodparam">void )
final public
EvTimer timer
( float $after
, float
$repeat , <span
class="type">callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
public void verify ( <span class="methodparam">void )
}
属性
data
Custom data attached to loop
backend
Readonly . The
backend flags indicating the
event backend in use.
is_default_loop
Readonly . true if it is the default event loop.
iteration
The current iteration count of the loop. See <span
class="methodname">Ev::iteration
pending
The number of pending watchers. 0 indicates that there are no
watchers pending.
io_interval
Higher io_interval allows libev to spend more time collecting <span
class="classname">EvIo events, so more events can be handled per
iteration, at the cost of increasing latency. Timeouts (both <span
class="classname">EvPeriodic and <span
class="classname">EvTimer ) will not be affected. Setting this to
a non-zero value will introduce an additional sleep() call into most
loop iterations. The sleep time ensures that libev will not poll for
EvIo events more often than once per this
interval, on average. Many programs can usually benefit by setting the
io_interval to a value near 0.1 , which is often enough for
interactive servers(not for games). It usually doesn't make much sense
to set it to a lower value than 0.01 , as this approaches the
timing granularity of most systems.
See also » FUNCTIONS CONTROLLING EVENT LOOPS .
timeout_interval
Higher timeout_interval allows libev to spend more time collecting
timeouts, at the expense of increased latency/jitter/inexactness(the
watcher callback will be called later). <span
class="classname">EvIo watchers will not be affected. Setting
this to a non-null value will not introduce any overhead in libev .
See also
» FUNCTIONS CONTROLLING EVENT LOOPS
.
depth
The recursion depth. See Ev::depth .
EvLoop::backend
Returns an integer describing the backend used by libev
说明
public int <span class="methodname">EvLoop::backend ( <span class="methodparam">void )
The same as Ev::backend , but for the loop instance.
参数
此函数没有参数。
返回值
Returns an integer describing the backend used by libev. See <span class="methodname">Ev::backend .
参见
- Ev::backend
EvLoop::check
Creates EvCheck object associated with the current event loop instance
说明
final public
EvCheck <span
class="methodname">EvLoop::check (
string $callback [, <span
class="methodparam"> string $data
[, string
$priority ]] )
Creates EvCheck object associated with the current event loop instance.
参数
All parameters have the same meaning as for <span class="methodname">EvCheck::__construct
返回值
Returns EvCheck object on success.
参见
- EvCheck::__construct
EvLoop::child
Creates EvChild object associated with the current event loop
说明
final public
EvChild <span
class="methodname">EvLoop::child (
string $pid , <span
class="methodparam"> string $trace ,
string $callback
[, string
$data [, <span
class="type">string $priority ]] )
Creates EvChild object associated with the current event loop.
参数
All parameters have the same meaning as for <span class="methodname">EvChild::__construct
返回值
Returns EvChild object on success.
参见
- EvChild::__construct
EvLoop::__construct
Constructs the event loop object
说明
public <span
class="methodname">EvLoop::__construct ([ <span
class="methodparam"> int $flags [,
mixed $data <span
class="initializer"> = NULL [, <span
class="methodparam"> float $io_interval
= 0.0 [, <span
class="methodparam"> float $timeout_interval
= 0.0 ]]]] )
Constructs the event loop object.
参数
flags
One of the event loop flags
data
Custom data associated with the loop.
io_interval
See io_interval
timeout_interval
See timeout_interval
返回值
Returns new EvLoop object.
参见
- EvLoop::defaultLoop
EvLoop::defaultLoop
Returns or creates the default event loop
说明
public <span
class="modifier">static EvLoop <span
class="methodname">EvLoop::defaultLoop ([ <span
class="methodparam"> int $flags <span
class="initializer"> = Ev::FLAG_AUTO [, <span
class="methodparam"> mixed $data <span
class="initializer"> = NULL [, <span
class="methodparam"> float $io_interval
= 0. [, <span
class="methodparam"> float $timeout_interval
= 0. ]]]] )
If the default event loop is not created, <span class="methodname">EvLoop::defaultLoop creates it with the specified parameters. Otherwise, it just returns the object representing previously created instance ignoring all the parameters.
参数
flags
One of the event loop flags
data
Custom data to associate with the loop.
io_collect_interval
See io_interval
timeout_collect_interval
See timeout_interval
返回值
Returns EvLoop object on success.
参见
- EvLoop::__construct
EvLoop::embed
Creates an instance of EvEmbed watcher associated with the current EvLoop object
说明
final public
EvEmbed <span
class="methodname">EvLoop::embed (
string $other [, <span
class="methodparam"> string $callback
[, string
$data [, <span
class="type">string $priority ]]] )
Creates an instance of EvEmbed watcher associated with the current EvLoop object.
参数
All parameters have the same meaning as for <span class="methodname">EvEmbed::__construct .
返回值
Returns EvEmbed object on success.
参见
- EvEmbed::__construct
EvLoop::fork
Creates EvFork watcher object associated with the current event loop instance
说明
final public
EvFork <span
class="methodname">EvLoop::fork (
callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Creates EvFork watcher object associated with the current event loop instance
参数
All parameters have the same meaning as for <span class="methodname">EvFork::__construct
返回值
Returns EvFork object on success.
参见
- EvFork::__construct
EvLoop::idle
Creates EvIdle watcher object associated with the current event loop instance
说明
final public
EvIdle <span
class="methodname">EvLoop::idle (
callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Creates EvIdle watcher object associated with the current event loop instance
参数
All the parameters have the same meaning as for <span class="methodname">EvIdle::__construct
返回值
Returns EvIdle object on success.
参见
- EvIdle::__construct
EvLoop::invokePending
Invoke all pending watchers while resetting their pending state
说明
public void EvLoop::invokePending ( <span class="methodparam">void )
Invoke all pending watchers while resetting their pending state.
参数
此函数没有参数。
返回值
没有返回值。
EvLoop::io
Create EvIo watcher object associated with the current event loop instance
说明
final public
EvIo <span
class="methodname">EvLoop::io ( <span
class="type">mixed $fd ,
int $events , <span
class="methodparam"> callable $callback
[, mixed
$data = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Create EvIo watcher object associated with the current event loop instance.
参数
All parameters have the same meaning as for <span class="methodname">EvIo::__construct
返回值
Returns EvIo object on success.
参见
- EvIo::__construct
EvLoop::loopFork
Must be called after a fork
说明
public void EvLoop::loopFork ( <span class="methodparam">void )
Must be called after a fork in the child, before entering or
continuing the event loop. An alternative is to use
Ev::FLAG_FORKCHECK which calls this function automatically, at
some performance loss (refer to the
» libev documentation
).
参数
此函数没有参数。
返回值
没有返回值。
EvLoop::now
Returns the current "event loop time"
说明
public float EvLoop::now ( <span class="methodparam">void )
Returns the current "event loop time", which is the time the event loop received events and started processing them. This timestamp does not change as long as callbacks are being processed, and this is also the base time used for relative timers. You can treat it as the timestamp of the event occurring(or more correctly, libev finding out about it).
参数
此函数没有参数。
返回值
Returns time of the event loop in (fractional) seconds.
参见
- Ev::now
EvLoop::nowUpdate
Establishes the current time by querying the kernel, updating the time returned by EvLoop::now in the progress
说明
public void EvLoop::nowUpdate ( <span class="methodparam">void )
Establishes the current time by querying the kernel, updating the time returned by EvLoop::now in the progress. This is a costly operation and is usually done automatically within EvLoop::run .
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvLoop::now
- Ev::nowUpdate
EvLoop::periodic
Creates EvPeriodic watcher object associated with the current event loop instance
说明
final public
EvPeriodic <span
class="methodname">EvLoop::periodic (
float $offset , <span
class="methodparam"> float $interval
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Creates EvPeriodic watcher object associated with the current event loop instance
参数
All parameters have the same maening as for <span class="methodname">EvPeriodic::__construct
返回值
Returns EvPeriodic object on success.
参见
- EvPeriodic::__construct
EvLoop::prepare
Creates EvPrepare watcher object associated with the current event loop instance
说明
final public
EvPrepare <span
class="methodname">EvLoop::prepare (
callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Creates EvPrepare watcher object associated with the current event loop instance
参数
All parameters have the same maening as for <span class="methodname">EvPrepare
返回值
Returns EvPrepare object on success
参见
- EvPrepare::__construct
EvLoop::resume
Resume previously suspended default event loop
说明
public void EvLoop::resume ( <span class="methodparam">void )
EvLoop::suspend and <span class="methodname">EvLoop::resume methods suspend and resume a loop correspondingly.
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvLoop::suspend
- Ev::resume
EvLoop::run
Begin checking for events and calling callbacks for the loop
说明
public void
EvLoop::run ([ <span
class="methodparam"> int $flags <span
class="initializer"> = 0 ] )
Begin checking for events and calling callbacks for the current event
loop. Returns when a callback calls <span
class="methodname">Ev::stop method, or the flags are nonzero(in
which case the return value is true) or when there are no active
watchers which reference the loop( <span
class="methodname">EvWatcher::keepalive is true), in which
case the return value will be false. The return value can
generally be interpreted as if true, there is more work left to
do .
参数
flags
Optional parameter flags can be one of the following:
flags |
Description |
|---|---|
0 |
The default behavior described above |
Ev::RUN_ONCE |
Block at most one(wait, but don't loop) |
Ev::RUN_NOWAIT |
Don't block at all(fetch/handle events, but don't wait) |
See the run flag constants .
返回值
没有返回值。
参见
- EvLoop::stop
- Ev::run
EvLoop::signal
Creates EvSignal watcher object associated with the current event loop instance
说明
final public
EvSignal <span
class="methodname">EvLoop::signal (
int $signum , <span
class="methodparam"> callable $callback
[, mixed
$data = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Creates EvSignal watcher object associated with the current event loop instance
参数
All parameters have the same meaning as for <span class="methodname">EvSignal::__construct
返回值
Returns EvSignal object on success
参见
- EvSignal::__construct
EvLoop::stat
Creates EvStat watcher object associated with the current event loop instance
说明
final public
EvStat <span
class="methodname">EvLoop::stat (
string $path , <span
class="methodparam"> float $interval
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Creates EvStat watcher object associated with the current event loop instance
参数
All parameters have the same meaning as for <span class="methodname">EvSignal::__construct
返回值
Returns EvStat object on success
参见
- EvSignal::__construct
EvLoop::stop
Stops the event loop
说明
public void
EvLoop::stop ([ <span
class="methodparam"> int $how ] )
Stops the event loop
参数
how
One of Ev::BREAK_*
constants .
返回值
没有返回值。
参见
- EvLoop::run
- Ev::stop
EvLoop::suspend
Suspend the loop
说明
public void EvLoop::suspend ( <span class="methodparam">void )
EvLoop::suspend and <span class="methodname">EvLoop::resume methods suspend and resume a loop correspondingly.
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvLoop::resume
- Ev::suspend
EvLoop::timer
Creates EvTimer watcher object associated with the current event loop instance
说明
final public
EvTimer <span
class="methodname">EvLoop::timer (
float $after , <span
class="methodparam"> float $repeat ,
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Creates EvTimer watcher object associated with the current event loop instance
参数
All parameters have the same meaning as for <span class="methodname">EvTimer::__construct
返回值
Returns EvTimer object on success
参见
- EvTimer::__construct
EvLoop::verify
Performs internal consistency checks(for debugging)
说明
public void EvLoop::verify ( <span class="methodparam">void )
Performs internal consistency checks(for debugging libev ) and abort the program if any data structures were found to be corrupted.
参数
此函数没有参数。
返回值
没有返回值。
参见
- Ev::verify
简介
Periodic watchers are also timers of a kind, but they are very versatile.
Unlike EvTimer , <span class="classname">EvPeriodic watchers are not based on real time(or relative time, the physical time that passes) but on wall clock time(absolute time, calendar or clock). The difference is that wall clock time can run faster or slower than real time, and time jumps are not uncommon(e.g. when adjusting it).
EvPeriodic watcher can be configured to
trigger after some specific point in time. For example, if an <span
class="classname">EvPeriodic watcher is configured to trigger
"in 10 seconds" (e.g. EvLoop::now +
10.0 , i.e. an absolute time, not a delay), and the system clock
is reset to January of the previous year , then it will take a year or
more to trigger the event (unlike an <span
class="classname">EvTimer , which would still trigger roughly
10 seconds after starting it as it uses a relative timeout).
As with timers, the callback is guaranteed to be invoked only when the point in time where it is supposed to trigger has passed. If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones with later time-out values (but this is no longer true when a callback calls <span class="methodname">EvLoop::run recursively).
类摘要
EvPeriodic
class EvPeriodic <span class="ooclass"> extends EvWatcher {
/* 属性 */
public $offset ;
public $interval ;
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public void again ( <span class="methodparam">void )
public float at ( <span class="methodparam">void )
public <span
class="methodname">__construct (
float $offset , <span
class="methodparam"> string $interval
, callable
$reschedule_cb , <span
class="type">callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
final public
static <span
class="type">EvPeriodic <span
class="methodname">createStopped (
float $offset , <span
class="methodparam"> float $interval
, callable
$reschedule_cb , <span
class="type">callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
public void
set ( <span
class="type">float $offset ,
float $interval )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
属性
offset
When repeating, this contains the offset value, otherwise this is the
absolute point in time(the offset value passed to <span
class="methodname">EvPeriodic::set , although libev might
modify this value for better numerical stability).
interval
The current interval value. Can be modified any time, but changes only
take effect when the periodic timer fires or <span
class="methodname">EvPeriodic::again is being called.
EvPeriodic::again
Simply stops and restarts the periodic watcher again
说明
public void EvPeriodic::again ( <span class="methodparam">void )
Simply stops and restarts the periodic watcher again. This is only useful when attributes are changed.
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvTimer::again
EvPeriodic::at
Returns the absolute time that this watcher is supposed to trigger next
说明
public float EvPeriodic::at ( <span class="methodparam">void )
When the watcher is active, returns the absolute time that this watcher is supposed to trigger next. This is not the same as the offset argument to EvPeriodic::set or <span class="methodname">EvPeriodic::__construct , but indeed works even in interval mode.
参数
此函数没有参数。
返回值
Returns the absolute time this watcher is supposed to trigger next in seconds.
EvPeriodic::__construct
Constructs EvPeriodic watcher object
说明
public <span
class="methodname">EvPeriodic::__construct ( <span
class="methodparam"> float $offset ,
string $interval
, callable
$reschedule_cb , <span
class="type">callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Constructs EvPeriodic watcher object and starts it automatically. <span class="methodname">EvPeriodic::createStopped method creates stopped periodic watcher.
参数
offset
See
Periodic watcher operation modes
interval
See
Periodic watcher operation modes
reschedule_cb
Reschedule callback. You can pass null. See
Periodic watcher operation modes
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvPeriodic object on success.
范例
示例 #1 Periodic timer. Use reschedule callback
<?php
// Tick each 10.5 seconds
function reschedule_cb ($watcher, $now) {
return $now + (10.5. - fmod($now, 10.5));
}
$w = new EvPeriodic(0., 0., "reschedule_cb", function ($w, $revents) {
echo time(), PHP_EOL;
});
Ev::run();
?>
示例 #2 Periodic timer. Tick every 10.5 seconds starting at now
<?php
// Tick every 10.5 seconds starting at now
$w = new EvPeriodic(fmod(Ev::now(), 10.5), 10.5, NULL, function ($w, $revents) {
echo time(), PHP_EOL;
});
Ev::run();
?>
示例 #3 Hourly watcher
<?php
$hourly = EvPeriodic(0, 3600, NULL, function () {
echo "once per hour\n";
});
?>
参见
- Periodic watcher operation modes
- EvTimer
- EvPeriodic::createStopped
EvPeriodic::createStopped
Create a stopped EvPeriodic watcher
说明
final public
static <span
class="type">EvPeriodic <span
class="methodname">EvPeriodic::createStopped ( <span
class="methodparam"> float $offset ,
float $interval
, callable
$reschedule_cb , <span
class="type">callable $callback [, <span
class="methodparam"> mixed $data <span
class="initializer"> = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Create EvPeriodic object. Unlike <span class="methodname">EvPeriodic::__construct this method doesn't start the watcher automatically.
参数
offset
See
Periodic watcher operation modes
interval
See
Periodic watcher operation modes
reschedule_cb
Reschedule callback. You can pass null. See
Periodic watcher operation modes
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvPeriodic watcher object on success.
参见
- EvPeriodic::__construct
- EvTimer::createStopped
EvPeriodic::set
Configures the watcher
说明
public void
EvPeriodic::set ( <span
class="methodparam"> float $offset ,
float $interval
)
(Re-)Configures EvPeriodic watcher
参数
offset
The same meaning as for <span
class="methodname">EvPeriodic::__construct . See
Periodic watcher operation modes
interval
The same meaning as for <span
class="methodname">EvPeriodic::__construct . See
Periodic watcher operation modes
返回值
没有返回值。
简介
EvPrepare and <span class="classname">EvCheck watchers are usually used in pairs. EvPrepare watchers get invoked before the process blocks, EvCheck afterwards.
It is not allowed to call EvLoop::run or similar methods or functions that enter the current event loop from either EvPrepare or <span class="classname">EvCheck watchers. Other loops than the current one are fine, however. The rationale behind this is that one don't need to check for recursion in those watchers, i.e. the sequence will always be: EvPrepare -> blocking -> <span class="classname">EvCheck , so having a watcher of each kind they will always be called in pairs bracketing the blocking call.
The main purpose is to integrate other event mechanisms into libev and their use is somewhat advanced. They could be used, for example, to track variable changes, implement custom watchers, integrate net-snmp or a coroutine library and lots more. They are also occasionally useful to cache some data and want to flush it before blocking.
It is recommended to give EvCheck
watchers highest( Ev::MAXPRI ) priority, to ensure that they are
being run before any other watchers after the poll (this doesn’t matter
for EvPrepare watchers).
Also, EvCheck watchers should not activate/feed events. While libev fully supports this, they might get executed before other EvCheck watchers did their job.
类摘要
EvPrepare
class EvPrepare <span class="ooclass"> extends EvWatcher {
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public <span
class="methodname">__construct (
string $callback [, <span
class="methodparam"> string $data
[, string
$priority ]] )
final public
static EvPrepare
createStopped ( <span
class="methodparam"> callable $callback
[, mixed
$data = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
EvPrepare::__construct
Constructs EvPrepare watcher object
说明
public <span
class="methodname">EvPrepare::__construct ( <span
class="methodparam"> string $callback
[, string
$data [, <span
class="type">string $priority ]] )
Constructs EvPrepare watcher object. And starts the watcher automatically. If need a stopped watcher consider using <span class="methodname">EvPrepare::createStopped
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvPrepare object on success.
参见
- EvCheck
EvPrepare::createStopped
Creates a stopped instance of EvPrepare watcher
说明
final public
static EvPrepare
EvPrepare::createStopped ( <span
class="methodparam"> callable $callback
[, mixed
$data = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
Creates a stopped instance of EvPrepare watcher. Unlike <span class="methodname">EvPrepare::__construct , this method doesn' start the watcher automatically.
参数
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Return EvPrepare object on success.
参见
- EvPrepare::__construct
- EvWatcher::start
简介
EvSignal watchers will trigger an event when the process receives a specific signal one or more times. Even though signals are very asynchronous, libev will try its best to deliver signals synchronously, i.e. as part of the normal event processing, like any other event.
There is no limit for the number of watchers for the same signal, but
only within the same loop, i.e. one can watch for SIGINT in the
default loop and for SIGIO in another loop, but it is not allowed
to watch for SIGINT in both the default loop and another loop at
the same time. At the moment, SIGCHLD is permanently tied to the
default loop.
If possible and supported, libev will install its handlers with SA_RESTART (or equivalent) behaviour enabled, so system calls should not be unduly interrupted. In case of a problem with system calls getting interrupted by signals, all the signals can be blocked in an EvCheck watcher and unblocked in a <span class="classname">EvPrepare watcher.
类摘要
EvSignal
class EvSignal extends EvWatcher {
/* 属性 */
public $signum ;
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public <span
class="methodname">__construct (
int $signum , <span
class="methodparam"> callable $callback
[, mixed
$data = null [, <span
class="methodparam"> int $priority <span
class="initializer"> = 0 ]] )
final public
static EvSignal
createStopped ( <span
class="methodparam"> int $signum ,
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
public void
set ( <span
class="type">int $signum )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
属性
signum
Signal number. See the constants exported by pcntl extension. See also
signal(7) man page.
EvSignal::__construct
Constructs EvSignal watcher object
说明
public <span
class="methodname">EvSignal::__construct ( <span
class="methodparam"> int $signum ,
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Constructs EvSignal watcher object and starts it automatically. For a stopped periodic watcher consider using <span class="methodname">EvSignal::createStopped method.
参数
signum
Signal number. See constants exported by pcntl extension. See also
signal(7) man page.
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvSignal object on success.
范例
示例 #1 Handle SIGTERM signal
<?php
$w = new EvSignal(SIGTERM, function ($watcher) {
echo "SIGTERM received\n";
$watcher->stop();
});
Ev::run();
?>
参见
- EvSignal::createStopped
EvSignal::createStopped
Create stopped EvSignal watcher object
说明
final public
static EvSignal
EvSignal::createStopped ( <span
class="methodparam"> int $signum ,
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Create stopped EvSignal watcher object. Unlike <span class="methodname">EvSignal::__construct , this method does't start the watcher automatically.
参数
signum
Signal number. See constants exported by pcntl extension. See also
signal(7) man page.
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvSignal object on success.
参见
- EvWatcher::start
- EvSignal::__construct
EvSignal::set
Configures the watcher
说明
public void
EvSignal::set ( <span
class="methodparam"> int $signum )
Configures the watcher.
参数
signum
Signal number. The same as for <span
class="methodname">EvSignal::__construct
返回值
没有返回值。
简介
EvStat monitors a file system path for attribute changes. It calls stat() on that path in regular intervals(or when the OS signals it changed) and sees if it changed compared to the last time, invoking the callback if it did.
The path does not need to exist: changing from "path exists" to "path
does not exist" is a status change like any other. The condition "path
does not exist" is signified by the 'nlink' item being 0(returned
by EvStat::attr method).
The path must not end in a slash or contain special components such as
'.' or .. . The path should be absolute: if it is relative
and the working directory changes, then the behaviour is undefined.
Since there is no portable change notification interface available, the
portable implementation simply calls stat() regularly on the path to
see if it changed somehow. For this case a recommended polling interval
can be specified. If one specifies a polling interval of 0.0
(highly recommended) then a suitable, unspecified default value will be
used(which could be expected to be around 5 seconds, although this might
change dynamically). libev will also impose a minimum interval which
is currently around 0.1 , but that’s usually overkill.
This watcher type is not meant for massive numbers of <span class="classname">EvStat watchers, as even with OS-supported change notifications, this can be resource-intensive.
类摘要
EvStat
class EvStat extends EvWatcher {
/* 属性 */
public $path ;
public $interval ;
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public array attr ( <span class="methodparam">void )
public <span
class="methodname">__construct (
string $path , <span
class="methodparam"> float $interval
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
final public
static void
createStopped ( <span
class="methodparam"> string $path ,
float $interval
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
public void prev ( <span class="methodparam">void )
public void
set ( <span
class="type">string $path ,
float $interval )
public bool stat ( <span class="methodparam">void )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
属性
interval
Readonly . Hint on how quickly a change is expected to be detected and
should normally be specified as 0.0 to let libev choose a
suitable value.
path
Readonly . The path to wait for status changes on.
EvStat::attr
Returns the values most recently detected by Ev
说明
public array EvStat::attr ( <span class="methodparam">void )
Returns array of the values most recently detected by Ev
参数
此函数没有参数。
返回值
Returns array with the values most recently detect by Ev(without actual stat 'ing):
| Key | Description |
|---|---|
'dev' |
ID of device containing file |
'ino' |
inode number |
'mode' |
protection |
'nlink' |
number of hard links |
'uid' |
user ID of owner |
'size' |
total size, in bytes |
'gid' |
group ID of owner |
'rdev' |
device ID (if special file) |
'blksize' |
blocksize for file system I/O |
'blocks' |
number of 512B blocks allocated |
'atime' |
time of last access |
'ctime' |
time of last status change |
'mtime' |
time of last modification |
See stat(2) man page for details.
范例
示例 #1 Monitor changes of /var/log/messages
<?php
// Use 10 second update interval.
$w = new EvStat("/var/log/messages", 8, function ($w) {
echo "/var/log/messages changed\n";
$attr = $w->attr();
if ($attr['nlink']) {
printf("Current size: %ld\n", $attr['size']);
printf("Current atime: %ld\n", $attr['atime']);
printf("Current mtime: %ld\n", $attr['mtime']);
} else {
fprintf(STDERR, "`messages` file is not there!");
$w->stop();
}
});
Ev::run();
?>
参见
- EvStat::prev
- EvStat::stat
EvStat::__construct
Constructs EvStat watcher object
说明
public <span
class="methodname">EvStat::__construct ( <span
class="methodparam"> string $path ,
float $interval
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Constructs EvStat watcher object and starts the watcher automatically.
参数
path
The path to wait for status changes on.
interval
Hint on how quickly a change is expected to be detected and should
normally be specified as 0.0 to let libev choose a suitable
value.
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvStat watcher object on succes.
范例
示例 #1 Monitor changes of /var/log/messages
<?php
// Use 10 second update interval.
$w = new EvStat("/var/log/messages", 8, function ($w) {
echo "/var/log/messages changed\n";
$attr = $w->attr();
if ($attr['nlink']) {
printf("Current size: %ld\n", $attr['size']);
printf("Current atime: %ld\n", $attr['atime']);
printf("Current mtime: %ld\n", $attr['mtime']);
} else {
fprintf(STDERR, "`messages` file is not there!");
$w->stop();
}
});
Ev::run();
?>
EvStat::createStopped
Create a stopped EvStat watcher object
说明
final public
static void
EvStat::createStopped ( <span
class="methodparam"> string $path ,
float $interval
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Creates EvStat watcher object, but doesn't start it automatically(unlike EvStat::__construct ).
参数
path
The path to wait for status changes on.
interval
Hint on how quickly a change is expected to be detected and should
normally be specified as 0.0 to let libev choose a suitable
value.
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns a stopped EvStat watcher object on success.
参见
- EvStat::__construct
- EvWatcher::start
EvStat::prev
Returns the previous set of values returned by EvStat::attr
说明
public void EvStat::prev ( <span class="methodparam">void )
Just like EvStat::attr , but returns the previous set of values.
参数
此函数没有参数。
返回值
Returns an array with the same structure as the array returned by <span class="methodname">EvStat::attr . The array contains previously detected values.
参见
- EvStat::attr
- EvStat::stat
EvStat::set
Configures the watcher
说明
public void
EvStat::set (
string $path , <span
class="methodparam"> float $interval
)
Configures the watcher.
参数
path
The path to wait for status changes on.
interval
Hint on how quickly a change is expected to be detected and should
normally be specified as 0.0 to let libev choose a suitable
value.
返回值
没有返回值。
EvStat::stat
Initiates the stat call
说明
public bool EvStat::stat ( <span class="methodparam">void )
Initiates the stat call(updates internal cache). It stats(using lstat
) the path specified in the watcher and sets to the values found.
参数
此函数没有参数。
返回值
Returns true if path exists. Otherwise false.
参见
- EvStat::attr
- EvStat::prev
简介
EvTimer watchers are simple relative timers that generate an event after a given time, and optionally repeating in regular intervals after that.
The timers are based on real time, that is, if one registers an event that times out after an hour and resets the system clock to January last year , it will still time out after(roughly) one hour. "Roughly" because detecting time jumps is hard, and some inaccuracies are unavoidable.
The callback is guaranteed to be invoked only after its timeout has passed (not at, so on systems with very low-resolution clocks this might introduce a small delay). If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones of the same priority with later time-out values (but this is no longer true when a callback calls <span class="methodname">EvLoop::run recursively).
The timer itself will do a best-effort at avoiding drift, that is, if a
timer is configured to trigger every 10 seconds, then it will
normally trigger at exactly 10 second intervals. If, however, the
script cannot keep up with the timer because it takes longer than those
10 seconds to do) the timer will not fire more than once per event
loop iteration.
类摘要
EvTimer
class EvTimer extends EvWatcher {
/* 属性 */
public $repeat ;
public $remaining ;
/* 继承的属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public void again ( <span class="methodparam">void )
public <span
class="methodname">__construct (
float $after , <span
class="methodparam"> float $repeat ,
callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
final public
static EvTimer
createStopped ( <span
class="methodparam"> float $after ,
float $repeat
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
public void
set ( <span
class="type">float $after ,
float $repeat )
/* 继承的方法 */
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
public void EvWatcher::start ( <span class="methodparam">void )
public void EvWatcher::stop ( <span class="methodparam">void )
}
属性
repeat
If repeat is 0.0 , then it will automatically be stopped once the
timeout is reached. If it is positive, then the timer will automatically
be configured to trigger again every repeat seconds later, until stopped
manually.
remaining
Returns the remaining time until a timer fires. If the timer is active,
then this time is relative to the current event loop time, otherwise
it's the timeout value currently configured.
That is, after instanciating an EvTimer
with an after value of 5.0 and repeat value of 7.0 ,
remaining returns 5.0 . When the timer is started and one second
passes, remaining will return 4.0 . When the timer expires and
is restarted, it will return roughly 7.0 (likely slightly less as
callback invocation takes some time too), and so on.
EvTimer::again
Restarts the timer watcher
说明
public void EvTimer::again ( <span class="methodparam">void )
This will act as if the timer timed out and restart it again if it is repeating. The exact semantics are:
-
if the timer is pending, its pending status is cleared.
-
if the timer is started but non-repeating, stop it (as if it timed out).
-
if the timer is repeating, either start it if necessary (with the
repeatvalue), or reset the running timer to therepeatvalue.
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvWatcher::stop
EvTimer::__construct
Constructs an EvTimer watcher object
说明
public <span
class="methodname">EvTimer::__construct ( <span
class="methodparam"> float $after ,
float $repeat
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Constructs an EvTimer watcher object.
参数
after
Configures the timer to trigger after after seconds.
repeat
If repeat is 0.0 , then it will automatically be stopped once the
timeout is reached. If it is positive, then the timer will automatically
be configured to trigger again every repeat seconds later, until stopped
manually.
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvTimer object on success.
范例
示例 #1 Simple timers
<?php
// Create and start timer firing after 2 seconds
$w1 = new EvTimer(2, 0, function () {
echo "2 seconds elapsed\n";
});
// Create and launch timer firing after 2 seconds repeating each second
// until we manually stop it
$w2 = new EvTimer(2, 1, function ($w) {
echo "is called every second, is launched after 2 seconds\n";
echo "iteration = ", Ev::iteration(), PHP_EOL;
// Stop the watcher after 5 iterations
Ev::iteration() == 5 and $w->stop();
// Stop the watcher if further calls cause more than 10 iterations
Ev::iteration() >= 10 and $w->stop();
});
// Create stopped timer. It will be inactive until we start it ourselves
$w_stopped = EvTimer::createStopped(10, 5, function($w) {
echo "Callback of a timer created as stopped\n";
// Stop the watcher after 2 iterations
Ev::iteration() >= 2 and $w->stop();
});
// Loop until Ev::stop() is called or all of watchers stop
Ev::run();
// Start and look if it works
$w_stopped->start();
echo "Run single iteration\n";
Ev::run(Ev::RUN_ONCE);
echo "Restart the second watcher and try to handle the same events, but don't block\n";
$w2->again();
Ev::run(Ev::RUN_NOWAIT);
$w = new EvTimer(10, 0, function() {});
echo "Running a blocking loop\n";
Ev::run();
echo "END\n";
?>
以上例程的输出类似于:
2 seconds elapsed
is called every second, is launched after 2 seconds
iteration = 1
is called every second, is launched after 2 seconds
iteration = 2
is called every second, is launched after 2 seconds
iteration = 3
is called every second, is launched after 2 seconds
iteration = 4
is called every second, is launched after 2 seconds
iteration = 5
Run single iteration
Callback of a timer created as stopped
Restart the second watcher and try to handle the same events, but don't block
Running a blocking loop
is called every second, is launched after 2 seconds
iteration = 8
is called every second, is launched after 2 seconds
iteration = 9
is called every second, is launched after 2 seconds
iteration = 10
END
参见
- EvTimer::createStopped
- EvPeriodic
- » ev_timer - relative and optionally repeating timeouts
- » Be smart about timeouts
EvTimer::createStopped
Creates EvTimer stopped watcher object
说明
final public
static EvTimer
EvTimer::createStopped ( <span
class="methodparam"> float $after ,
float $repeat
, callable
$callback [, <span
class="type">mixed $data =
null [, <span
class="type">int $priority =
0 ]] )
Creates EvTimer stopped watcher object. Unlike <span class="methodname">EvTimer::__construct , this method doesn't start the watcher automatically.
参数
after
Configures the timer to trigger after after seconds.
repeat
If repeat is 0.0 , then it will automatically be stopped once the
timeout is reached. If it is positive, then the timer will automatically
be configured to trigger again every repeat seconds later, until stopped
manually.
callback
See
Watcher callbacks
.
data
Custom data associated with the watcher.
priority
Watcher priority
返回值
Returns EvTimer watcher object on success.
范例
示例 #1 Monotor changes of /var/log/messages. Avoid missing updates by means of one second delay
<?php
$timer = EvTimer::createStopped(0., 1.02, function ($w) {
$w->stop();
$stat = $w->data;
// 1 second after the most recent change of the file
printf("Current size: %ld\n", $stat->attr()['size']);
});
$stat = new EvStat("/var/log/messages", 0., function () use ($timer) {
// Reset timer watcher
$timer->again();
});
$timer->data = $stat;
Ev::run();
?>
参见
- EvTimer::__construct
- EvPeriodic
EvTimer::set
Configures the watcher
说明
public void
EvTimer::set ( <span
class="methodparam"> float $after ,
float $repeat
)
Configures the watcher
参数
after
Configures the timer to trigger after after seconds.
repeat
If repeat is 0.0 , then it will automatically be stopped once the
timeout is reached. If it is positive, then the timer will automatically
be configured to trigger again every repeat seconds later, until stopped
manually.
返回值
没有返回值。
简介
EvWatcher is a base class for all watchers( EvCheck , <span class="classname">EvChild etc.). Since <span class="classname">EvWatcher 's constructor is <span class="modifier">abstract , one can't(and don't need to) create EvWatcher objects directly.
类摘要
EvWatcher
abstract class EvWatcher {
/* 属性 */
public $is_active ;
public $data ;
public $is_pending ;
public $priority ;
/* 方法 */
public int <span class="methodname">clear ( void )
abstract <span class="modifier">public <span class="methodname">__construct ( <span class="methodparam">void )
public void
feed ( <span
class="type">int $revents )
public EvLoop getLoop ( <span class="methodparam">void )
public void
invoke (
int $revents )
public bool
keepalive ([
bool $value ] )
public void
setCallback (
callable $callback )
public void start ( <span class="methodparam">void )
public void stop ( <span class="methodparam">void )
}
属性
is_active
Readonly . true if the watcher is active. false otherwise.
data
User custom data associated with the watcher
is_pending
Readonly .true if the watcher is pending, i.e. it has
outstanding events, but its callback has not yet been invoked.
false otherwise. As long, as a watcher is pending(but not active),
one must not change its priority.
priority
int between Ev::MINPRI and
Ev::MAXPRI . Pending watchers with higher priority will be invoked
before watchers with lower priority, but priority will not keep watchers
from being executed(except for EvIdle
watchers). EvIdle watchers provide
functionality to suppress invocation when higher priority events are
pending.
EvWatcher::clear
Clear watcher pending status
说明
public int <span class="methodname">EvWatcher::clear ( <span class="methodparam">void )
If the watcher is pending, this method clears its pending status and
returns its revents bitset(as if its callback was invoked). If the
watcher isn't pending it does nothing and returns 0 .
Sometimes it can be useful to "poll" a watcher instead of waiting for its callback to be invoked, which can be accomplished with this function.
参数
此函数没有参数。
返回值
In case if the watcher is pending, returns revents bitset as if the
watcher callback
had been invoked. Otherwise returns 0 .
EvWatcher::__construct
Abstract constructor of a watcher object
说明
abstract <span class="modifier">public <span class="methodname">EvWatcher::__construct ( <span class="methodparam">void )
EvWatcher::__construct is an abstract constructor of a watcher object implemented in the derived classes.
EvWatcher::feed
Feeds the given revents set into the event loop
说明
public void
EvWatcher::feed ( <span
class="methodparam"> int $revents )
Feeds the given revents set into the event loop, as if the specified event had happened for the watcher.
参数
revents
Bit mask of watcher
received events .
返回值
没有返回值。
EvWatcher::getLoop
Returns the loop responsible for the watcher
说明
public EvLoop EvWatcher::getLoop ( <span class="methodparam">void )
Returns the loop responsible for the watcher
参数
此函数没有参数。
返回值
Returns EvLoop event loop object responsible for the watcher.
EvWatcher::invoke
Invokes the watcher callback with the given received events bit mask
说明
public void
EvWatcher::invoke ( <span
class="methodparam"> int $revents )
Invokes the watcher callback with the given received events bit mask.
参数
revents
Bit mask of watcher
received events .
返回值
没有返回值。
EvWatcher::keepalive
Configures whether to keep the loop from returning
说明
public bool
EvWatcher::keepalive ([ <span
class="methodparam"> bool $value ]
)
Configures whether to keep the loop from returning. With keepalive
value set to false the watcher won't keep <span
class="methodname">Ev::run / <span
class="methodname">EvLoop::run from returning even though the
watcher is active.
Watchers have keepalive value true by default.
Clearing keepalive status is useful when returning from <span class="methodname">Ev::run / <span class="methodname">EvLoop::run just because of the watcher is undesirable. It could be a long running UDP socket watcher or so.
参数
value
With keepalive value set to false the watcher won't keep <span
class="methodname">Ev::run / <span
class="methodname">EvLoop::run from returning even though the
watcher is active.
返回值
Returns the previous state.
范例
示例 #1 Register an I/O watcher for some UDP socket but do not keep the event loop from running just because of that watcher.
<?php
$udp_socket = ...
$udp_watcher = new EvIo($udp_socket, Ev::READ, function () { /* ... */ });
$udp_watcher->keepalive(FALSE);
?>
EvWatcher::setCallback
Sets new callback for the watcher
说明
public void
EvWatcher::setCallback ( <span
class="methodparam"> callable $callback
)
Sets new callback for the watcher
参数
callback
See
Watcher callbacks
.
返回值
没有返回值。
EvWatcher::start
Starts the watcher
说明
public void EvWatcher::start ( <span class="methodparam">void )
Marks the watcher as active. Note that only active watchers will receive events.
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvWatcher::stop
EvWatcher::stop
Stops the watcher
说明
public void EvWatcher::stop ( <span class="methodparam">void )
Marks the watcher as inactive. Note that only active watchers will receive events.
参数
此函数没有参数。
返回值
没有返回值。
参见
- EvWatcher::start