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
- <?php
- /*
- Generating queues
- */
- Generates a queue file name in microseconds. Because there will be multiple queues, a identifier is added to distinguish the individual queues
- function MT ($identifier =' default ')
- {
- return sprintf ("%.6f.%s",strtok (Microtime (), ") +strtok ("),$identifier);
- }
- while (1) //In practice try not to do while (1) do not want while (1) Remember the task is done to break
- {
- if (count (glob ('./queue/*.identifier ')) >=10) //queue maximum length, no limit to the hard drive may be unbearable oh.
- {
- Sleep (1); //Remember to sleep here, otherwise the queue is full CPU high
- continue;
- }
- $url = ' www. '. Time (). '. com '; //For example, I generated a URL with a timestamp
- echo "$url \ r \ n";
- $fp = fopen ('./queue/'. MT (' identifier '),' W ');
- Fwrite ($fp,$url);
- Fclose ($fp);
- Sleep (1); //Do not need sleep here, I sleep because my task is too simple.
- }
- ?>
read.php:
PHP code
- <?php
- /*
- Processing queues
- */
- while (1) //The actual program is best not while (1) if while (1), remember to finish processing the task to break
- {
- if ($queue = glob ('./queue/*.identifier '))
- {
- $q = array_shift ($queue);
- $url = file_get_contents ($q);
- echo $url." \ r \ n ";
- Unlink ($q);
- }
- Sleep (1); //Do you want to sleep or sleep here how long you feel.
- }
- ?>
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
- <?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--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
- <?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 would get rid of a queue
- Function Queue_destroy (&$queue) {
- //Since PHP is nice-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 is 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 the 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 would 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 is done.
- Queue_destroy ($myqueue);
- ?>
Simple PHP Task queue