usleep()

(PHP 4, PHP 5, PHP 7)

以指定的微秒数延迟执行

说明usleep(int$micro_seconds):void

以指定的微秒数延缓程序的执行。

参数$micro_seconds暂停的时间以微秒计。1微秒(micro second)是百万分之一秒。

返回值

没有返回值。

范例

Example #1usleep()例子<?php

// Current time

echo date('h:i:s') . "\n";

// wait for 2 seconds

usleep(2000000);

// back!

echo date('h:i:s') . "\n";

?>

以上例程会输出:11:13:28

11:13:30

参见WARNING!!

the snippet below by 'Marius (mm at co-operation dot de)' is NOT a usleep and it will keep the CPU at 100% running. why people keep posting that crap is a complete mystery for me.

the idea of sleep and usleep is that by letting the cpu run a few idle cycles so the other programs can have some cycles run of their own. what results in better response times and lower overall system-load. so if you have to wait for something, go to sleep for a few seconds instead of occupying the cpu while doing absolute nothing but waitting.On both MacOS X and Linux the usleep() call seems to consume CPU cycles, whereas sleep() and time_nanosleep() do not. This was the same on PHP 5.3.29 and 5.5.29.

I used a loop with just a call to sleep/usleep/time_nanosleep, and compared them all with an empty loop. Obviously the empty loop consumed 99% of the CPU, sleep used 0%, usleep used 3% for 1000ms and 6% for 100ms, and time_nanosleep used 0% for both 500ms and 1000ms.If you're using Windows then you maybe are in trouble with usleep if you really need to use it.

The Bernie's microdelay function using fsockopen does not work properly, and the fclose doesn't help much.

I don't know if network connections go strange, but I know it does not work since you've made more than 2000 - 3000 calls to it, so it's not a reliable solution in 'long life' php scripts, or these are the issues of the microdelay function in my PHP and PHP-GTK applications.

Though another solution should be found, and googling a bit I fount a WinAPI function: Sleep.

So I get with this snippet wich works fine for me, you get milliseconds precission but the more important, it works for long-run scripts and of course, it does not waste any CPU cycles.

dl('php_w32api.dll');

$GLOBALS['win32api'] =& new win32;

// USleep alternative for Windows and PHP4:

$GLOBALS['win32api']->registerfunction("long Sleep (long dwMillisecods) From kernel32.dll");

// Now you can call the function from everywhere in your script: $GLOBALS['win32api']->Sleep(milliseconds);

for ($msec = 2000; $msec > 0; $msec = $msec - 125) {

echo "Hi. Next one in $msec msec.\n";

$GLOBALS['win32api']->Sleep($msec);

}Note that this function has an overhead!

Example:

for ($i = 0; $i < 1000000; ++$i)

{

usleep(1);

}

?>

This block is running about 70 seconds on my server.

Script take about 70 microseconds for every usleep() function call.A word of warning about the microdelay() code posted that uses the fsockopen - if you use this is a loop that delays for small periods you will very quickly run out of sockets/socket buffer space. And then your network connections go very strange......It should be noted that Windows machines have a resolution of either 10 mS or 15 mS (depending on the chipset implementation and HAL used) when using the Sleep() function in kernel32.dll. This means that your average error will be either 5 or 7.5 mS. This is not ordinarily a problem unless you really NEED to sleep for less time than the granularity provided by Windows.Should be noted that functions that loop really fast to create a delay also consume 100% CPU while doing the loop. Try creating a dummy loop that goes 100000 times, watch it choke your machine. If you really need usleep() don't use windows.To monitor a scripts CPU ussage and avoid any nasty CPU gobbling loops you can use this function (will not work with windows or safe mode) I know it works on FreeBSD:

function phpmon($max)

{

$cmd = `ps -Unobody -r -o%cpu`;

$lines = explode("\n", $cmd);

$usage = substr($lines[1], 0, strpos($lines[1], "."));

$sleeprate = 500;

while ($usage >= $max)

{

$cmd = `ps -Unobody -r -o%cpu`;

$lines = explode("\n", $cmd);

$usage = substr($lines[1], 0, strpos($lines[1], "."));

usleep($sleeprate);

}

}

phpmon($MAX);

where $MAX is the maximum CPU you want the process to consume. e-mail me with any improvements/suggestions.

I have noticed that this consumes a lot of system CPU (at least in my limited testing) possibly from all of the system calls or the huge mathematical functions I used to test the effectiveness of the script.I have spent DAYS trying to create a reliable usleep() replacement for Windows.

I have only this to offer:

As commented by someone else already, the gettimeofday() method used below is useless - PHP will use all available CPU power doing nothing.

The fsockopen() method apparently is also useless - as someone else commented, an fclose() was missing in the original post, but this apparently does not solve the problem. After calling the function about 50 or so times, fsockopen() returns immidiately, without any delay - and watching a process monitor in Windows, you can then watch the process taking up increasingly more memory, until eventually PHP aborts (or crashes) when it reaches maximum.

The win32api-method is also a no-go ... after calling the Sleep function a few hundred times (during which memory usage will also go up every time due to a memory leak somewhere), PHP will cause an exception and Windows will terminate it.

I have given up - I don't think there is any viable solution to this problem under PHP 4.

If you need this function, upgrade your project to PHP 5.

Or settle for 1-second delays with the sleep() function.

These, unfortunately, seem to be your only options...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;

}I have no idea why nobody came up with this yet, but there is an efficient way to reproduce usleep() under windows:

function usleep_win($msec) {

$usec = $msec * 1000;

socket_select($read = NULL, $write = NULL, $sock = array(socket_create (AF_INET, SOCK_RAW, 0)), 0, $usec);

}

?>

* Doesnt busy wait

* Doesnt eat memory

* Works for millions of repetitions

* Seems to be rather efficient

It seems that the average error (on my machine) is about 5ms (it sleeps 5ms more than intended) thats probably due to code execution as well as kernel timers.

A socket without any purpose is highly unlikely to every cause an exception, so socket_select will always sleep until the timeout is hit.I want to create a daemon/Linux service. Here is an example of how to run a process that has "throttle control"

// You must set these

//

// max_execution_time = 0

// max_input_time = 0

function doProcess() {

echo "Start"."\n";

usleep(10000);

echo "Stop"."\n";

return false;

}

function manageProcess() {

// Setup data

$runsPerMinute = 200;

$maxMinuteAverage = 5;

$waitIfNotWorking = 120; // seconds

// Conversion

$microsPerSecond = 1000000;

// Statistical Info

$currentMinute = 0;

$minute = -1;

$countPerMinute = array();

$sumPerMinute = array();

// Totals

$totalProcessTime = 0;

$totalCounts = 0;

while (true) {

$timestart = microtime();

$performedWork = doProcess();

$timeend = microtime();

if (!$performedWork) {

// Statistical Info

$currentMinute = 0;

$minute = -1;

$countPerMinute = array();

$sumPerMinute = array();

sleep($waitIfNotWorking);

} else {

$ts = split(" ",$timestart);

$te = split(" ",$timeend);

$te[0] = ($te[0] * $microsPerSecond) - ($ts[0] * $microsPerSecond);

$te[1] = ($te[1] - $ts[1]) * $microsPerSecond;

$processTime = $te[0] + $te[1];

if (date("i")<>$minute) { // We are NOT in the same minute

// Reset the new minute

$minute = date("i");

$currentMinute = ($currentMinute+1) % $maxMinuteAverage;

// Remove Statistical Information from the minute we are expiring.

if (isset($countPerMinute[$currentMinute])) {

$totalProcessTime = $totalProcessTime - $sumPerMinute[$currentMinute];

$totalCounts = $totalCounts - $countPerMinute[$currentMinute];

}

$countPerMinute[$currentMinute] = 0;

$sumPerMinute[$currentMinute] = 0;

}

$countPerMinute[$currentMinute] = $countPerMinute[$currentMinute] + 1;

$sumPerMinute[$currentMinute] = $sumPerMinute[$currentMinute] + $processTime;

$totalCounts = $totalCounts + 1;

$totalProcessTime = $totalProcessTime + $processTime;

$averageRuntime = round($totalProcessTime / $totalCounts);

$waitTime = (($microsPerSecond*60) / $runsPerMinute) - $averageRuntime;

usleep($waitTime);

}

}

}

manageProcess();

php7 kernel32,usleep()相关推荐

  1. php7 扩展 性能测试,php7简单性能测试

    (一) 一个简单的测试 看看php7跑多快(就不罗列电脑配置了,因为环境是一样的) 1 测试代码: /** * test.php ; compare php5.6.7 with php7.0.0alp ...

  2. PHP7.3中fileinfo怎么安装与开启

    摘要:安装lumen时创建项目时报错,需开启fileinfo 操作方法如下: cd /usr/local/src/php-7.3.13/ext/fileinfo/usr/local/php/bin/p ...

  3. CentOS7 php7.0 升级到php7.3

    不要问我为什么要升级,我不是运维.如果你也不是运维的话,而且是公司的服务器的话,那你还是要慎重啊,我他么的就这样填了一天的坑,简单记录一下这坑爹的一天 备份之前的php7.0(这很重要,非常重要) 如 ...

  4. FastAdmin扩展PHPEXCEL,PHP7.3高版本兼容问题

    THINKPHP扩展PHPEXCEL与PHP7.3高版本兼容问题 框架:THINKPHP5,PHPEXCEL版本:1.81 无法导出EXCEL原因为Shared/OLE.php第290行使用conti ...

  5. wampserver下升级php7

    wampserver下升级php7 1.下载php7 http://windows.php.net/download#php-7.0 选择 VC14 x86 Thread Safe 64位选X64 3 ...

  6. mac自带php7降级,MAC更新自带php版本到7.0

    说一下我最近更新mac自带php版本的经历,我是按照网上的教程更新的,主要参考的是这篇博客http://blog.csdn.net/takunha/article/details/52484769,通 ...

  7. 7 centos 源码安装samba_在CentOS7.6里编译安装PHP7.4(最新版),很详细

    拿到一个空的服务器,首先我们就要来用起来啦! 首先要先下载并编译安装PHP最新版本,请到php官网下载: 这里我首先在系统里在新建存储源码包的文件夹,比如我存放在这里 下载安装PHP7.4之前,你要先 ...

  8. php编译7教程,PHP7 快速编译安装

    PHP7正式版发布啦, 之前没有安装过的,都来安装试一试 . 即将发布的ThinkPHP5 在PHP7环境下也完全兼容, 佩服鸟哥把兼容性做得这么好 快速编译安装PHP7步骤: 第一步: 安装必要一些 ...

  9. cryptojs php 互通_关于PHP7和CryptoJS的AES加密方式互通

    对于PHP7与CryptoJS的AES加密的方式互通代码不能正确执行,我的测试代码为: JS端:let iv = CryptoJS.enc.Utf8.parse('1111111111111111') ...

最新文章

  1. clevo风扇调速软件_YVP变频调速电机
  2. netty系列之:netty中的ByteBuf详解
  3. 如何在windows机器上安装apache ab
  4. java 对接支付宝支付
  5. Luogu 3267 [JLOI2016/SHOI2016]侦察守卫
  6. GO、Rust 这些新一代高并发编程语言为何都极其讨厌共享内存?
  7. 背包九讲-第三讲 多重背包
  8. 0基础入门VTD-实操静态道路建模3
  9. 移动终端浏览器初始设置apple-mobile-web-app-capable(转)
  10. 《如何阅读一本书》笔记
  11. 阿里轻量应用服务器搭建ftp服务器
  12. ACM2021辽宁省赛:CDEFGILM
  13. sakila-dwh-schema文件
  14. linux桌面图标恢复,恢复Linux桌面下方面板上显示最小化的图标的方法
  15. 有测试狗狗好坏的软件吗,想要养狗的朋友们请一定看完全文,测试一下自己适不适合养狗 ​...
  16. 财路网每日原创推送:币圈灰姑娘,BSV弱市该如何逆袭
  17. paypal支付 paypal网站付款标准版问题解决
  18. Hibernate从0开始,入门到放弃,一文上手
  19. 超级坦克大战1990 - 坦克大战超难版
  20. 记一次 Cause: java.sql.SQLException: The user specified as a definer (‘XXX‘@‘%‘) does not exist的解决办法

热门文章

  1. 003竞品分析的思考、理解和一些框架
  2. JSON.stringify格式化Json字符串
  3. 【方法】Latex多行公式及编号
  4. 安装win10出现“计算机意外的重新启动或遇到错误。Windows安装无法继续。若要安装Windows,请单击“确定”重新启动计算机,然后安装系统。”
  5. python2和python3的区别
  6. CheckBoxList详细用法
  7. U1C2 文本预处理
  8. 2022-2-14至2022-2-19周报
  9. python能开发公众号吗_python如何编写公众号
  10. redhat7.6配置网络yum源