PHP uses inotify for queue processing

Source: Internet
Author: User
First, we need to reach the following consensus:

/DEV/SHM is the memory directory under Linux, obviously in this directory is built in memory, the default can use 50% of memory, when the memory is low, you can switch to disk by swap.

INotify is the file system event monitoring under Linux, which can meet various file monitoring needs, provides interfaces such as Inotify_init,inotify_read, requires linux2.6 or more cores (uname-a view), It seems that Cygwin can't be realized.

PHP does not provide support for inotify extensions by default, so we need to download the build installation via PECL.

Download inotify (http://pecl.php.net/package/inotify), Unzip and install:

TAR-XVF INOTIFY-0.1.6.TGZCD inotify-0.1.6/usr/local/php/bin/phpize./configure--with-php-config=/usr/local/php/ Bin/php-configmake && make Install

Then load inotify.so in the php.ini file to see if there is a successful load that can be viewed through Php-i|grep inotify.

Then in the/DEV/SHM to establish the memory directory, because the queue processing is a high speed, put to disk will have a certain amount of I/O time consumption, we build/dev/shm/inotify directory, and then use PHP to write a dead loop demo to monitor the directory, in addition, by changing the/dev/ Shm/inotify the file or property of the directory to see the results:

!--? php$notify = Inotify_init (); $rs = Inotify_add_watch ($notify, '/dev/shm/ INotify ', in_create);//in_create indicates that only the creation of new files is monitored, and the specific parameter list can be found at the inotify of the manual.             if (! $rs) {die (' fail to Watch/dev/shm/inotify ');}        while (1) {$files = Inotify_read ($notify);        Print_r ($files); Echo ' Continue to process next event ';}  

Using the INotify module is more flexible and resource-saving than looping and the scan directory continuously, and at Inotify_read, it is blocked until no events are received, so there is no operation where there is no action required to loop execution.

Attempts to create a new Test.txt file in/dev/shm/inotify, which returns an array of all the files in Inotify_read, such as:

array ([0] = = Array ([WD] = 1 [Mask] = 25 6 [Cookie] = 0 [name] + test.txt)) 
  • 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.