這篇文章主要介紹了關於PHP操作Beanstalkd的方法及參數註釋,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
Beanstalk,一個高性能、輕量級的分散式記憶體佇列系統,最初設計的目的是想透過後台非同步執行耗時的任務來降低高容量Web應用系統的頁面訪問延遲,支援過有9.5 million用戶的Facebook Causes應用程式。
後來開源,現在有PostRank大規模部署和使用,每天處理百萬級任務。 Beanstalkd是典型的類Memcached設計,協議和使用方式都是同樣的風格,所以使用過memcached的用戶會覺得Beanstalkd似曾相識。
job典型的生命週期
put reserve delete -----> [READY] ---------> [RESERVED] --------> *poof*
job可能的狀態遷移
put with delay release with delay ----------------> [DELAYED] <------------. | | kick | (time passes) | | | put v reserve | delete -----------------> [READY] ---------> [RESERVED] --------> *poof* ^ ^ | | | \ release | | | `-------------' | | | | kick | | | | bury | [BURIED] <---------------' | | delete `--------> *poof*
安裝略過,如需請參考點擊參考
註:需要安裝Composer,教學連結Composer安裝方法
用於操作Beanstalkd
的PHP
第三方函式庫,安裝略過,如需請參考點擊參考
<?php require __DIR__ . '/vendor/autoload.php'; use Pheanstalk\Pheanstalk; /** * 实例化beanstalk * 参数依次为:ip地址 端口号默认11300 连接超时时间 是否长连接 */ $pheanstalk = new Pheanstalk('127.0.0.1', 11300, 3, false); ?>
Beanstalkd狀態##
$stats = $pheanstalk->stats();返回:
Pheanstalk\Response\ArrayResponse Object ( [_name:Pheanstalk\Response\ArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [current-jobs-urgent] => 0 //当前存在优先级的任务数 [current-jobs-ready] => 0 //当前准备就绪的任务数 [current-jobs-reserved] => 0 //当前处于阻塞的任务数 [current-jobs-delayed] => 0 //当前处于延迟状态的任务数 [current-jobs-buried] => 0 //当前预留的任务数 [cmd-put] => 0 //cmd命令为累计运行次数 [cmd-peek] => 0 [cmd-peek-ready] => 0 [cmd-peek-delayed] => 0 [cmd-peek-buried] => 0 [cmd-reserve] => 0 [cmd-reserve-with-timeout] => 0 [cmd-delete] => 0 [cmd-release] => 0 [cmd-use] => 0 [cmd-watch] => 0 [cmd-ignore] => 0 [cmd-bury] => 0 [cmd-kick] => 0 [cmd-touch] => 0 [cmd-stats] => 1 [cmd-stats-job] => 0 [cmd-stats-tube] => 0 [cmd-list-tubes] => 3 [cmd-list-tube-used] => 0 [cmd-list-tubes-watched] => 0 [cmd-pause-tube] => 0 [job-timeouts] => 0 //超时的任务 [total-jobs] => 0 //任务总数 [max-job-size] => 65535 //任务字符串大小 [current-tubes] => 1 //当前的管道数 [current-connections] => 1 //当前打开的连接数 [current-producers] => 0 //当前生产者数量 [current-workers] => 0 //当前消费者数量 [current-waiting] => 0 //发出reserved指令,但没有响应的数量 [total-connections] => 4 //累计链接数量 [pid] => 530 //Beanstalkd进程id [version] => 1.10 [rusage-utime] => 0.003545 [rusage-stime] => 0.007473 [uptime] => 105855 //运行时间(秒) [binlog-oldest-index] => 0 [binlog-current-index] => 0 [binlog-records-migrated] => 0 [binlog-records-written] => 0 [binlog-max-size] => 10485760 [id] => 4199e3eca8bfdea8 [hostname] => lmmlwendeMacBook-Air.local ) )
目前的管道清單
$listTubes = $pheanstalk->listTubes();
查看管道的詳細資訊
$stats = $pheanstalk->statsTube();返回:
Pheanstalk\Response\ArrayResponse Object ( [_name:Pheanstalk\Response\ArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [name] => default //当前管道名 [current-jobs-urgent] => 0 //当前管道存在优先级的任务数 [current-jobs-ready] => 0 //当前管道准备就绪的任务书 [current-jobs-reserved] => 0 //当前管道处于阻塞的任务数 [current-jobs-delayed] => 0 //当前管道处于延迟状态的任务数 [current-jobs-buried] => 0 //当前管道预留的任务数 [total-jobs] => 0 //当前管道总任务数 [current-using] => 1 //当前管道生产者数量 [current-watching] => 1 //当前管道消费者数量 [current-waiting] => 0 //发出reserved指令,但没有响应的数量 [cmd-delete] => 0 [cmd-pause-tube] => 0 [pause] => 0 [pause-time-left] => 0 ) )
查看任務的詳細資訊#
$job = $pheanstalk->watch('default')->reserve(); $job_stats = $pheanstalk->statsJob($job);返回:
Pheanstalk\Response\ArrayResponse Object ( [_name:Pheanstalk\Response\ArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [id] => 1 //任务job ID [tube] => test //所处管道 [state] => reserved //当前状态 [pri] => 1024 //任务优先级(默认1024) [age] => 469 //任务存活时间(秒) [delay] => 0 //任务延迟时间(秒) [ttr] => 60 //任务执行时间 [time-left] => 59 //任务在reserve状态维持的秒数 [file] => 0 // binlog-4 默认-0 [reserves] => 2 //总共reserve次数 [timeouts] => 0 //任务超时次数 [releases] => 0 //重设任务次数 [buries] => 0 //预留次数 [kicks] => 0 //释放预留任务次数 ) )
查看任務的詳細資訊(透過ID)
$job = $pheanstalk->peek(1); $job_stats = $pheanstalk->statsJob($job);Beanstalkd生產者方法
指定需要使用的管道
$tube = $pheanstalk->useTube('default');
向管道插入資料
$tube = $pheanstalk->useTube('default'); $put = $tube->put( 'hello, beanstalk', // 任务内容 1024, // 任务的优先级 10, // 不等待直接放到ready队列中 60 // 处理任务的时间 );或:
$pheanstalk->putInTube('default', 'test1', 1024, 10, 60);Beanstalkd消費者方法
#監聽管道
$tube = $pheanstalk->watch('user');
移除不需要監聽的管道
$tube = $pheanstalk->watch('user')->ignore('default');
# #以堵塞的方式監聽管道$job = $pheanstalk->watch('user')->reserve(4); //堵塞时间为4秒
列出所有已經監聽的管道$pheanstalk->listTubesWatched();
watch reserve 方法$pheanstalk->reserveFromTube('default')
刪除目前任務$job = $pheanstalk->watch('default')->reserve();
$pheanstalk->delete($job);
將目前任務重新放入管道$job = $pheanstalk->watch('default')->reserve();
$pheanstalk->release($job);
為任務續命(當處理任務的時間小於目前任務執行時間時)$job = $pheanstalk->watch('default')->reserve();
$pheanstalk->touch($job);
//TODO
將任務預留$job = $pheanstalk->watch('default')->reserve();
$pheanstalk->bury($job);
#將預留任務釋放(變成reday狀態)$job = $pheanstalk->peekBuried('default');
$pheanstalk->kickJob($job);
批次將預留任務釋放$pheanstalk->userTube('default')->kick(999); //将id小于999的预留任务全部释放
讀取目前準備就緒的任務(ready)$job = $pheanstalk->peekReady('default');
讀取目前處於延遲狀態的任務(delayed)$job = $pheanstalk->peekDelayed('default');
對管道設定延遲$pheanstalk->pauseTube('default', 100); //设置100秒延迟
取消對管道的延遲#$pheanstalk->resumeTube('default');
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
PHP實作的內網穿透應用Spike重構完成php呼叫phantomjs給微信小程式分享以上是PHP操作Beanstalkd的方法及參數註釋的詳細內容。更多資訊請關注PHP中文網其他相關文章!