Created
October 15, 2014 10:01
-
-
Save davidecavaliere/52ae9037c085d5f6bdbd to your computer and use it in GitHub Desktop.
non blocking i/o php socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$urls = ['www.amazon.com',...]; | |
foreach ($url as $ip => $url) { | |
// create a stream that returns immediately | |
// STREAM_CLIENT_CONNECT _MUST_ be passed | |
// STREAM_CLIENT_ASYNC_CONNECT says create connection | |
// asyncrhronously | |
$socket = stream_socket_client("tcp://$ip:80", $errno, $errstr, 0, | |
STREAM_CLIENT_CONNECT, STREAM_CLIENT_ASYNC_CONNECT); | |
// set stream as non blockcing with 0, deafult is 1 | |
stream_set_blocking($socket, 0); | |
$sockets[(int) $socket] = $socket; | |
$requests[(int) $socket] = $socket; | |
} | |
while (!empty($sockets)) { | |
// run the loop | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment