Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

php timer

License

NotificationsYou must be signed in to change notification settings

mrtwenty/timer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php定时器,参考了workerman源码,由于workerman源码太过复杂, 故而抽取了一些出来,重新整理出来,实现一个单进程(守护进程)的定时器。

原理

  1. 利用pcntl,守护进程化
  2. 利用stream_select的超时机制,来实现sleep,如果有event扩展的话,优先使用event扩展
  3. 定时器是时间堆的方式实现,利用php的spl的优先队列

使用方式

1.安装

composer require mrtwenty/timer

2.编写index.php

<?phprequire__DIR__ .'/vendor/autoload.php';usetimer\Daemon;$timer = Daemon::runAll();//测试执行 timer类functionmicrotime_float(){list($usec,$sec) =explode("",microtime());returnbcadd($usec,$sec,3);}$timer->add(0.5,function () {if (Daemon::getOS() ===OS_TYPE_WIN) {echomicrotime_float() ."\n";    }else {file_put_contents("/tmp/test.txt",microtime_float() ."\n",FILE_APPEND);    }});$timer->add(1,function () {if (Daemon::getOS() ===OS_TYPE_WIN) {echomicrotime_float() ."once\n";    }else {file_put_contents("/tmp/test.txt",microtime_float() ."once\n",FILE_APPEND);    }},false);$timer->loop();
  1. 在cli环境上执行:
php index.php

[8]ページ先頭

©2009-2025 Movatter.jp