標籤:des ar io os 使用 sp for java on
文章太長,不作過多介紹,反正,文章的頭部就說明了大概的意思。。。
原文如下:
寫了一個簡單的隊列任務處理。多進程任務,非同步任務可能會用到這個(主要是命令列應用)
比如,任務的某個一個環節速度十分不穩定,可能執行幾秒,也可能執行幾分鐘,
我就可以把那個環節包括前面的部分扔進隊列,多跑幾個進程,同時往隊列裡面寫。
然後後面比較快的環節只跑一個處理任務就OK了。讓整體速度達到更好的效果。
write.php: 將任務寫入隊列
PHP代碼
- <?php
- /*
- 產生隊列
- */
- //用微秒產生隊列檔案名稱。因為會有多個隊列,所以加了一個identifier來區分各個隊列
- function mt($identifier=‘default‘)
- {
- return sprintf("%.6f.%s",strtok(microtime(),‘ ‘)+strtok(‘‘),$identifier);
- }
-
- while(1) //實際中盡量不要while(1) 非要while(1)記得任務完成要break
- {
- if(count(glob(‘./queue/*.identifier‘))>=10) //隊列最大長度,不限制的話硬碟可能會受不了哦。
- {
- sleep(1);//記住這裡要sleep,否則隊列滿了cpu佔用很高
- continue;
- }
- $url = ‘www.‘.time().‘.com‘; //隨便舉個例子,我用時間戳記產生了一個網址
- echo "$url\r\n";
- $fp = fopen(‘./queue/‘.mt(‘identifier‘),‘w‘);
- fwrite($fp,$url);
- fclose($fp);
- sleep(1);//這裡不需要sleep,我sleep是因為我的工作太簡單。
- }
- ?>
read.php:
PHP代碼
- <?php
- /*
- 處理隊列
- */
-
- while(1) //實際程式最好不要while(1)如果while(1),記得處理完任務要break
- {
- if($queue = glob(‘./queue/*.identifier‘))
- {
- $q = array_shift($queue);
- $url = file_get_contents($q);
- echo $url."\r\n";
- unlink($q);
- }
- sleep(1);//這裡要不要sleep或sleep多久自己憑感覺來。
- }
- ?>
相關的資料:雙向隊列
baidu和google上沒有查到PHP雙向隊列的資料,搜尋到java的雙向隊列定義如下:雙向隊列(雙端隊列)就像是一個隊列,但是你可以在任何一端添加或移除元素。
而雙端隊列是一種資料結構,定義如下:
A deque is a data structure consisting of a list of items, on which the following operations are possible.
* push(D,X) -- insert item X on the rear end of deque D.
* pop(D) -- remove the front item from the deque D and return it.
* inject(D,X) -- insert item X on the front end of deque D.
* eject(D) -- remove the rear item from the deque D and return it.
Write routines to support the deque that take O(1) time per operation.
翻譯:雙端隊列(deque)是由一些項的表組成的資料結構,對該資料結構可以進行下列操作:
push(D,X) 將項X 插入到雙端隊列D的前端
pop(D) 從雙端隊列D中刪除前端項並將其返回
inject(D,X) 將項X插入到雙端隊列D的尾端
eject(D) 從雙端隊列D中刪除尾端項並將其返回
編寫支援雙端隊伍的常式,每種操作均花費O(1)時間
百度百科:(deque,全名double-ended queue)是一種具有隊列和棧的性質的資料結構。雙端隊列中的元素可以從兩端彈出,其限定插入和刪除操作在表的兩端進行。
轉貼一個使用Php數組函數實現該功能的代碼:
PHP代碼
- <?php
- //Input limit double-ende queue
- class DoubleEndedQueue1 {
- var $queue = array();
- function add($var){
- return array_push($this->queue, $var);
- }
- function frontRemove(){
- return array_shift($this->queue);
- }
- function rearRemove(){
- return array_pop($this->queue);
- }
- }
-
- //Output limit double-ende queue
- class DoubleEndedQueue2 {
- var $queue = array();
- function remove(){
- return array_pop($this->queue);
- }
- function frontAdd($var){
- return array_unshift($this->queue, $var);
- }
- function rearAdd($var){
- return array_push($this->queue, $var);
- }
- }
-
- //Test code
- $q = new DoubleEndedQueue1;
- $q->add(‘aaa‘);
- $q->add(‘bbb‘);
- $q->add(‘ccc‘);
- $q->add(‘ddd‘);
-
- echo $q->frontRemove();
- echo "<br>";
- echo $q->rearRemove();
- echo "<br>";
- print_r($q->queue);
- ?>
array_push -- 將一個或多個單元壓入數組的末尾(入棧)
array_unshift -- 在數組開頭插入一個或多個單元
array_pop -- 將數組最後一個單元彈出(出棧)
array_shift -- 將數組開頭的單元移出數組
// 來自 PHP5 in Practice (U.S.)Elliott III & Jonathan D.Eisenhamer
PHP代碼
- <?php
- // A library to implement queues in PHP via arrays
- // The Initialize function creates a new queue:
- function &queue_initialize() {
- // In this case, just return a new array
- $new = array();
- return $new;
- }
- // The destroy function will get rid of a queue
- function queue_destroy(&$queue) {
- // Since PHP is nice to us, we can just use unset
- unset($queue);
- }
- // The enqueue operation adds a new value unto the back of the queue
- function queue_enqueue(&$queue, $value) {
- // We are just adding a value to the end of the array, so can use the
- // [] PHP Shortcut for this. It‘s faster than using array_push
- $queue[] = $value;
- }
- // Dequeue removes the front of the queue and returns it to you
- function queue_dequeue(&$queue) {
- // Just use array unshift
- return array_shift($queue);
- }
- // Peek returns a copy of the front of the queue, leaving it in place
- function queue_peek(&$queue) {
- // Return a copy of the value found in front of queue
- // (at the beginning of the array)
- return $queue[0];
- }
- // Size returns the number of elements in the queue
- function queue_size(&$queue) {
- // Just using count will give the proper number:
- return count($queue);
- }
- // Rotate takes the item on the front and sends it to the back of the queue.
- function queue_rotate(&$queue) {
- // Remove the first item and insert it at the rear.
- $queue[] = array_shift($queue);
- }
- // Let‘s use these to create a small queue of data and manipulate it.
- // Start by adding a few words to it:
- $myqueue =& queue_initialize();
- queue_enqueue($myqueue, ‘Opal‘);
- queue_enqueue($myqueue, ‘Dolphin‘);
- queue_enqueue($myqueue, ‘Pelican‘);
- // The queue is: Opal Dolphin Pelican
- // Check the size, it should be 3
- echo ‘<p>Queue size is: ‘, queue_size($myqueue), ‘</p>‘;
- // Peek at the front of the queue, it should be: Opal
- echo ‘<p>Front of the queue is: ‘, queue_peek($myqueue), ‘</p>‘;
- // Now rotate the queue, giving us: Dolphin Pelican Opal
- queue_rotate($myqueue);
- // Remove the front element, returning: Dolphin
- echo ‘<p>Removed the element at the front of the queue: ‘,
- queue_dequeue($myqueue), ‘</p>‘;
- // Now destroy it, we are done.
- queue_destroy($myqueue);
- ?>
簡單的PHP的任務隊列