簡單的PHP的任務隊列

來源:互聯網
上載者:User

標籤:des   ar   io   os   使用   sp   for   java   on   

文章太長,不作過多介紹,反正,文章的頭部就說明了大概的意思。。。
原文如下:
寫了一個簡單的隊列任務處理。多進程任務,非同步任務可能會用到這個(主要是命令列應用)
比如,任務的某個一個環節速度十分不穩定,可能執行幾秒,也可能執行幾分鐘,
我就可以把那個環節包括前面的部分扔進隊列,多跑幾個進程,同時往隊列裡面寫。
然後後面比較快的環節只跑一個處理任務就OK了。讓整體速度達到更好的效果。

write.php: 將任務寫入隊列

PHP代碼
  1. <?php  
  2. /* 
  3. 產生隊列 
  4. */  
  5. //用微秒產生隊列檔案名稱。因為會有多個隊列,所以加了一個identifier來區分各個隊列  
  6. function mt($identifier=‘default‘)  
  7. {  
  8.         return sprintf("%.6f.%s",strtok(microtime(),‘ ‘)+strtok(‘‘),$identifier);  
  9. }  
  10.   
  11. while(1) //實際中盡量不要while(1) 非要while(1)記得任務完成要break  
  12. {  
  13.         if(count(glob(‘./queue/*.identifier‘))>=10) //隊列最大長度,不限制的話硬碟可能會受不了哦。  
  14.         {  
  15.                 sleep(1);//記住這裡要sleep,否則隊列滿了cpu佔用很高  
  16.                 continue;  
  17.         }  
  18.         $url = ‘www.‘.time().‘.com‘; //隨便舉個例子,我用時間戳記產生了一個網址  
  19.         echo "$url\r\n";  
  20.         $fp = fopen(‘./queue/‘.mt(‘identifier‘),‘w‘);  
  21.         fwrite($fp,$url);  
  22.         fclose($fp);  
  23.         sleep(1);//這裡不需要sleep,我sleep是因為我的工作太簡單。  
  24. }  
  25. ?>  

read.php:

PHP代碼
  1. <?php  
  2. /* 
  3. 處理隊列 
  4. */  
  5.   
  6. while(1) //實際程式最好不要while(1)如果while(1),記得處理完任務要break  
  7. {  
  8.         if($queue = glob(‘./queue/*.identifier‘))  
  9.         {  
  10.                 $q = array_shift($queue);  
  11.                 $url = file_get_contents($q);  
  12.                 echo $url."\r\n";  
  13.                 unlink($q);  
  14.         }  
  15.         sleep(1);//這裡要不要sleep或sleep多久自己憑感覺來。  
  16. }  
  17. ?>  

相關的資料:雙向隊列

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代碼
  1. <?php  
  2. //Input limit double-ende queue  
  3. class DoubleEndedQueue1 {  
  4. var $queue = array();  
  5. function add($var){  
  6.     return array_push($this->queue, $var);  
  7. }  
  8. function frontRemove(){  
  9.     return array_shift($this->queue);  
  10. }  
  11. function rearRemove(){  
  12.     return array_pop($this->queue);  
  13. }  
  14. }  
  15.   
  16. //Output limit double-ende queue  
  17. class DoubleEndedQueue2 {  
  18. var $queue = array();  
  19. function remove(){  
  20.     return array_pop($this->queue);  
  21. }  
  22. function frontAdd($var){  
  23.     return array_unshift($this->queue, $var);  
  24. }  
  25. function rearAdd($var){  
  26.     return array_push($this->queue, $var);  
  27. }  
  28. }  
  29.   
  30. //Test code  
  31. $q = new DoubleEndedQueue1;  
  32. $q->add(‘aaa‘);  
  33. $q->add(‘bbb‘);  
  34. $q->add(‘ccc‘);  
  35. $q->add(‘ddd‘);  
  36.   
  37. echo $q->frontRemove();  
  38. echo "<br>";  
  39. echo $q->rearRemove();  
  40. echo "<br>";  
  41. print_r($q->queue);  
  42. ?>  

array_push -- 將一個或多個單元壓入數組的末尾(入棧)
array_unshift -- 在數組開頭插入一個或多個單元
array_pop -- 將數組最後一個單元彈出(出棧)
array_shift -- 將數組開頭的單元移出數組

// 來自 PHP5 in Practice  (U.S.)Elliott III & Jonathan D.Eisenhamer

PHP代碼
    1. <?php  
    2. // A library to implement queues in PHP via arrays  
    3. // The Initialize function creates a new queue:  
    4. function &queue_initialize() {  
    5.     // In this case, just return a new array  
    6.     $new = array();  
    7.     return $new;  
    8. }  
    9. // The destroy function will get rid of a queue  
    10. function queue_destroy(&$queue) {  
    11.     // Since PHP is nice to us, we can just use unset  
    12.     unset($queue);  
    13. }  
    14. // The enqueue operation adds a new value unto the back of the queue  
    15. function queue_enqueue(&$queue, $value) {  
    16.     // We are just adding a value to the end of the array, so can use the  
    17.     //  [] PHP Shortcut for this.  It‘s faster than using array_push  
    18.     $queue[] = $value;  
    19. }  
    20. // Dequeue removes the front of the queue and returns it to you  
    21. function queue_dequeue(&$queue) {  
    22.     // Just use array unshift  
    23.     return array_shift($queue);  
    24. }  
    25. // Peek returns a copy of the front of the queue, leaving it in place  
    26. function queue_peek(&$queue) {  
    27.     // Return a copy of the value found in front of queue  
    28.     //  (at the beginning of the array)  
    29.     return $queue[0];  
    30. }  
    31. // Size returns the number of elements in the queue  
    32. function queue_size(&$queue) {  
    33.     // Just using count will give the proper number:  
    34.     return count($queue);  
    35. }  
    36. // Rotate takes the item on the front and sends it to the back of the queue.  
    37. function queue_rotate(&$queue) {  
    38.     // Remove the first item and insert it at the rear.  
    39.     $queue[] = array_shift($queue);  
    40. }  
    41. // Let‘s use these to create a small queue of data and manipulate it.  
    42. // Start by adding a few words to it:  
    43. $myqueue =& queue_initialize();  
    44. queue_enqueue($myqueue, ‘Opal‘);  
    45. queue_enqueue($myqueue, ‘Dolphin‘);  
    46. queue_enqueue($myqueue, ‘Pelican‘);  
    47. // The queue is: Opal Dolphin Pelican  
    48. // Check the size, it should be 3  
    49. echo ‘<p>Queue size is: ‘, queue_size($myqueue), ‘</p>‘;  
    50. // Peek at the front of the queue, it should be: Opal  
    51. echo ‘<p>Front of the queue is: ‘, queue_peek($myqueue), ‘</p>‘;  
    52. // Now rotate the queue, giving us: Dolphin Pelican Opal  
    53. queue_rotate($myqueue);  
    54. // Remove the front element, returning: Dolphin  
    55. echo ‘<p>Removed the element at the front of the queue: ‘,  
    56.     queue_dequeue($myqueue), ‘</p>‘;  
    57. // Now destroy it, we are done.  
    58. queue_destroy($myqueue);  
    59. ?>  

簡單的PHP的任務隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.