inotify阻止但超时

inotify阻止但超时

本文介绍了php inotify阻止但超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对php使用pecl扩展名,并使用inotify_read()函数来检测文件中的更改.

I want to use the pecl extension to php and use the inotify_read() function to detect changes in a file.

作为故障保险,我想为inotify_read函数指定一个超时值,以免万一引发事件并错过事件时我不会永远阻塞.

As a fail safe, I would like to specify a timeout value to the inotify_read function, just so I don't wind up blocking forever, in case an event is raised and is missed.

有人知道如何使用stream_select函数在指定的秒数内进行阻止,但是如果inotify_read上引发了事件,则会立即返回.

Does anyone know how to use the stream_select function to block for a specified number of seconds, but return immediately if an event is raised on the inotify_read.

我知道有一种方法可以执行inotify_read非阻塞,但是我不想坐在那儿进行轮询,而且我不希望在文件更改发生与通知时之间存在时间间隔通过它.

I know there is way to perform the inotify_read non-blocking, but I don't want to sit there and poll, and I don't want the lag between when the file change happens vs. when I'll be informed by it.

我能够使用pcntl_alarm中断系统调用,但我希望能够减轻强度.

I was able to use pcntl_alarm to interrupt the the system call, but I was hoping for something less intense.

推荐答案

像pecl inotify_init()函数一样,它返回围绕基础文件描述符的php_stream包装器.因此,是的,您应该能够使用stream_select()等待某些信号来通知inotify描述符.

Looks like the pecl inotify_init() function returns a php_stream wrapper around the underlying file descriptor. So yes, you should be able to use stream_select() to wait for something to signal the inotify descriptor.

类似以下的方法应该起作用:

Something like the following should work:

$in = inotify_init();
stream_set_blocking($in, false); // probably a good idea to make it non-blocking
$r = array($in);
$timeout = 10;
$n = stream_select($r, $w = array(), $e = array(), $timeout);
if ($n == 0) {
   // Timed out, so do something else
} else {
   // We know that inotify_read will not block; use it and process
   // the results
}

这篇关于php inotify阻止但超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:55