note 72284 added to function.usleep
| From: | Mike at SevenCode dot com | Date: | Thu, 11 Jan 2007 11:36:14 +0000 |
| Subject: | note 72284 added to function.usleep | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121552@lists.php.net to get a copy of this message | ||
Dude you are SO the man for that code snippet. It worked like a charm. I just wanted to point out a
couple things and offer my own improvement.
1. If you're like me, you were probably wondering why the socket had to keep being recreated on
each call, and why you couldn't just create a static socket. Its because socket_select assumes
you're passing in a pointer, and will alter the variable on return to reflect the actual
sockets that were changed.
2. I couldn't figure out for the life of me why socket_select wasn't defined. Its because
you hadn't enabled the right extension in php.ini
Ok so heres my slight improvement. The only real thing I did is use a static variable for the
socket, to avoid creating a brand new socket on each call of this function. I'm not sure if
socket creation will cause things to crash down the line like the other problems reported on here,
but if you ask me better safe then sorry.
function Sleeper($mSec)
{
// For dummies like me who spent 5 minutes
// wondering why socket_create wasn't defined
if(!function_exists('socket_create')){
die("Please enable extension php_sockets.dll");
}
// So the socket is only created once
static $socket=false;
if($socket===false){
$socket=array(socket_create(AF_INET,SOCK_RAW,0));
}
$pSock=$socket;
// Calc time
$uSex = $mSec * 1000;
// Do the waiting
socket_select($read=NULL,$write=NULL,$pSock,0,$uSex);
// OCD
return true;
}
----
Server IP: 66.163.161.117
Probable Submitter: 72.134.68.196
----
Manual Page -- http://www.php.net/manual/en/function.usleep.php
Edit -- https://master.php.net/note/edit/72284
Del: integrated -- https://master.php.net/note/delete/72284/integrated
Del: useless -- https://master.php.net/note/delete/72284/useless
Del: bad code -- https://master.php.net/note/delete/72284/bad+code
Del: spam -- https://master.php.net/note/delete/72284/spam
Del: non-english -- https://master.php.net/note/delete/72284/non-english
Del: in docs -- https://master.php.net/note/delete/72284/in+docs
Del: other reasons-- https://master.php.net/note/delete/72284
Reject -- https://master.php.net/note/reject/72284
Search -- https://master.php.net/manage/user-notes.php