Simple PHP Task queue

Source: Internet
Author: User
Tags glob strtok vars

The article is too long, do not introduce too much, anyway, the head of the article explains the approximate meaning ...
The original text reads as follows:
Wrote a simple queue task to handle. Multi-process tasks, which may be used by asynchronous tasks (mainly command line applications)
For example, the speed of one part of a task is very erratic, may take a few seconds, or it may take several minutes,
I can throw that link in front of the queue, run a few more processes, and write to the queue.
Then the relatively fast link only to run a processing task is OK. Let the overall speed achieve better results.

write.php: Writing a task to a queue

PHP code
  1. <?php
  2. /*
  3. Generating queues
  4. */
  5. Generates a queue file name in microseconds. Because there will be multiple queues, a identifier is added to distinguish the individual queues
  6. function MT ($identifier =' default ')
  7. {
  8. return sprintf ("%.6f.%s",strtok (Microtime (), ") +strtok ("),$identifier);
  9. }
  10. while (1) //In practice try not to do while (1) do not want while (1) Remember the task is done to break
  11. {
  12. if (count (glob ('./queue/*.identifier ')) >=10) //queue maximum length, no limit to the hard drive may be unbearable oh.
  13. {
  14. Sleep (1); //Remember to sleep here, otherwise the queue is full CPU high
  15. continue;
  16. }
  17. $url = ' www. '. Time (). '. com '; //For example, I generated a URL with a timestamp
  18. echo "$url \ r \ n";
  19. $fp = fopen ('./queue/'. MT (' identifier '),' W ');
  20. Fwrite ($fp,$url);
  21. Fclose ($fp);
  22. Sleep (1);  //Do not need sleep here, I sleep because my task is too simple.
  23. }
  24. ?>

read.php:

PHP code
  1. <?php
  2. /*
  3. Processing queues
  4. */
  5. while (1) //The actual program is best not while (1) if while (1), remember to finish processing the task to break
  6. {
  7. if ($queue = glob ('./queue/*.identifier '))
  8. {
  9. $q = array_shift ($queue);
  10. $url = file_get_contents ($q);
  11. echo $url."  \ r \ n ";
  12. Unlink ($q);
  13. }
  14. Sleep (1);  //Do you want to sleep or sleep here how long you feel.
  15. }
  16. ?>

Related information: two-way queue

Baidu and Google did not find the PHP two-way queue data, search Java two-way queue definition is as follows: two-way Queue (double-ended queue) is like a queue, but you can add or remove elements at either end.
The double-ended queue is a data structure that is defined as follows:
A deque is a data structure consisting of a list of items in which the following operations is 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, which take O (1) time per operation.

Translation: A double-ended queue (deque) is a data structure consisting of tables of items that can do the following:
Push (D,X) inserts item X into the front of the double-ended queue D
Pop (d) removes the front-end item from the double-ended queue D and returns it
Inject (D,X) inserts the item X at the end of the double-ended queue D
Eject (d) Remove the end item from the double-ended queue D and return it
Write a routine that supports two-ended teams, each of which takes an O (1) time

Baidu Encyclopedia: (deque, full name double-ended queue) is a data structure that has the nature of queues and stacks. Elements in a double-ended queue can be ejected from both ends, and their qualifying insert and delete operations are performed at both ends of the table.

Paste a code that uses the PHP array function to implement the function:

PHP code
  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. Output Limit Double-ende Queue
  16. Class DoubleEndedQueue2 {
  17. var $queue = Array ();
  18. function Remove () {
  19. return array_pop ($this->queue);
  20. }
  21. function Frontadd ($var) {
  22. return array_unshift ($this->queue, $var);
  23. }
  24. function Rearadd ($var) {
  25. return array_push ($this->queue, $var);
  26. }
  27. }
  28. Test Code
  29. $q = new DoubleEndedQueue1;
  30. $q->add (' AAA ');
  31. $q->add (' BBB ');
  32. $q->add (' CCC ');
  33. $q->add (' ddd ');
  34. echo $q->frontremove ();
  35. echo "<br>";
  36. echo $q->rearremove ();
  37. echo "<br>";
  38. Print_r ($q->queue);
  39. ?>

Array_push--presses one or more cells into the end of the array (into the stack)
Array_unshift--inserting one or more cells at the beginning of the array
Array_pop--pops the last element of the array (out of the stack)
Array_shift--Moves the cell at the beginning of the array to a group

From PHP5 in Practice (U.S.) Elliott III & Jonathan D.eisenhamer

PHP code
    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 would get rid of a queue
    10. Function Queue_destroy (&$queue) {
    11. //Since PHP is nice-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 is 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 the 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 would 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 is done.
    58. Queue_destroy ($myqueue);
    59. ?>

Simple PHP Task queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.