PHP queue principle and queue-based file writing case,

Source: Internet
Author: User

PHP queue principle and queue-based file writing case,

This article describes the principles of the PHP queue and the case of writing files based on the queue. We will share this with you for your reference. The details are as follows:

A queue is a linear table based on the first-in-first-out principle:

Join:

Departure:

PHP queue implementation:The first element serves as the head of the team, and the last element serves as the end of the team.

<? Php/*** the queue is so simple ** @ link */$ array = array ('php', 'java'); array_push ($ array, 'python '); // Input Queue array_shift ($ array); // output queue

What is a dual-end queue (or bidirectional queue) Deque, full name double-ended queue?

That is to say, an element can join or exit any segment of the queue. If we call these methods insertLeft () and insertRight (), and removeLeft () and removeRight (). If insertLeft () and removeLeft () methods are strictly prohibited (or right-segment operations are disabled), the dual-end queue function is the same as the stack function. The function of insertLeft () and removeRight () (or the opposite method) is the same as that of queue. Compared with stacks or queues, the dual-end queue is a multi-purpose data structure.

PHP implements dual-end queue

<? Phpclass Deque {public $ queue = array ();/** (tail) teaming **/public function addLast ($ value) {return array_push ($ this-> queue, $ value);}/** (tail) team-out **/public function removeLast () {return array_pop ($ this-> queue);}/** (header) queue **/public function addFirst ($ value) {return array_unshift ($ this-> queue, $ value);}/** (header) departure **/public function removeFirst () {return array_shift ($ this-> queue);}/** clear queue **/public function makeEmpty () {unset ($ this-> queue);}/** get the column header **/public function getFirst () {return reset ($ this-> queue );} /** get the end of a column **/public function getLast () {return end ($ this-> queue);}/** get length **/public function getLength () {return count ($ this-> queue );}}

Queue usage:

The queue can process data transmission and storage asynchronously. When you insert data frequently into the database and submit data frequently to the search engine, you can adopt a queue for asynchronous insertion. In addition, you can put slow processing logic and limited number of concurrent processing logic in the background through message queues for processing, such as FLV video conversion, sending SMS messages, and sending email.

Project case

There is a project here. Due to server permissions problems, you cannot install and install the queue program. The concurrency is over 300, and the maximum number of database connections of the service provider is 300, to solve this problem, I wrote a simple queue program. The Code is as follows:

Read queue code:

<? Phpset_time_limit (0); $ file_name3 = '3.txt '; // write the content read from the queue file to the file, in test, replace mysql database operations $ file3 = fopen ($ file_name3, 'A'); while (true) {$ c = FALSE; $ file_name = '1.txt '; // queue file $ file = fopen ($ file_name, 'R'); if (! Feof ($ f) {// get the first data $ a = fgets ($ file); if (! Empty ($ a) {$ c = TRUE; fwrite ($ file3, $ a); // here you can change it to database operation} fclose ($ file ); if ($ c) {// Delete the first data in the file exec ('sed-I \ '1d \ '/var/www/csv_ SQL/1.txt ');} sleep (1); if (time () >= strtotime ('000000') {exit ;}} fclose ($ file3 );

The read queue program is actually an endless loop program. If you do not set a close point, you will read the file cyclically.

Writing to a queue is simple, that is, writing to a file.

<? Phpset_time_limit (0); $ file_name2 = '2.txt '; // The $ file2 = fopen ($ file_name2, 'A') used in test to check whether the queue program is normal '); for ($ I = 1; $ I <11; $ I ++) {$ file_name = '1.txt '; // queue file $ file = fopen ($ file_name, 'A'); // The mode attribute of fopen must be a or a + $ str = $ I. '--'. rand (); fwrite ($ file, $ str. "\ n"); fwrite ($ file2, $ str. "\ n"); sleep (1); fclose ($ file) ;}fclose ($ file2 );

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.