©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PECL zmq >= 0.5.0)
ZMQPoll::poll — Poll the items
&$readable
, array &$writable
[, integer $timeout
= -1
] )
Polls the items in the current poll set. The readable and writable items are returned in the
readable
and writable
parameters.
ZMQPoll::getLastErrors() can be used to check if there were errors.
readable
Array where readable ZMQSockets/PHP streams are returned. The array will be cleared at the beginning of the operation.
writable
Array where writable ZMQSockets/PHP streams are returned. The array will be cleared at the beginning of the operation.
timeout
Timeout for the operation. -1 means that poll waits until at least one item has activity. Please note that starting from version 1.0.0 the poll timeout is defined in milliseconds, rather than microseconds.
Example #1 A ZMQPoll() example
Create a simple poll server
<?php
$context = new ZMQContext ();
$server = $context -> getSocket ( ZMQ :: SOCKET_REP );
$server -> bind ( "tcp://127.0.0.1:5555" );
$poll = new ZMQPoll ();
$id = $poll -> add ( $server , ZMQ :: POLL_IN | ZMQ :: POLL_OUT );
echo "Added object with id " . $id . "\n" ;
$readable = array();
$writable = array();
while ( true ) {
$events = 0 ;
try {
$events = $poll -> poll ( $readable , $writable , - 1 );
$errors = $poll -> getLastErrors ();
if ( count ( $errors ) > 0 ) {
foreach ( $errors as $error ) {
echo "Error polling object " . $error . "\n" ;
}
}
} catch ( ZMQPollException $e ) {
echo "poll failed: " . $e -> getMessage () . "\n" ;
}
if ( $events > 0 ) {
foreach ( $readable as $r ) {
try {
echo "Received message: " . $r -> recv () . "\n" ;
} catch ( ZMQException $e ) {
echo "recv failed: " . $e -> getMessage () . "\n" ;
}
}
foreach ( $writable as $w ) {
try {
$w -> send ( "Got it!" );
} catch ( ZMQException $e ) {
echo "send failed: " . $e -> getMessage () . "\n" ;
}
}
}
}
?>
Returns an integer representing amount of items with activity. Throws ZMQPollException on error.