有没有一种可移植的方法可以在 flock() 上设置超时?
Is there a portable way to put a timeout on flock()?
flock()
是 PHP 的 可移植 咨询文件锁定功能。他们明确宣传它甚至可以在 windows:
下运行
flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows).
我想在阻塞 flock()
上也设置一个 可移植 超时(并且不用忙等待 LOCK_NB
选项)。在 UNIX 中,这可以简单地通过设置一个将发送 SIGALRM
:
的警报来实现
pcntl_signal(SIGALRM, function() {});
pcntl_alarm(3);
try {
if (!flock($handle, LOCK_EX)) {
throw new \Exception("Timeout");
}
} finally {
pcntl_alarm(0);
pcntl_signal_dispatch();
pcntl_signal(SIGALRM, SIG_DFL);
}
是否有一种可移植的方法来为阻塞设置超时 flock()
?如果是,怎么做?
我认为在 Windows 上没有任何方法可以在没有繁忙的等待/轮询循环的情况下执行此操作。
PHP 实现 flock
on windows using LockFileEx
(see flock_compat.c:132
)。从这些类似的问题中可以看出,无法在 LockFileEx
上设置超时或取消等待 LockFileEx
请求的进程(即没有等同于 SIGALRM
此用例的信号):
- LockFile with timeout?(2011 年问)
Q) If I want to wait for file-lock with timeout, how would I go about it?
...
A) write a small loop to check the return code
- "LockFileEx can't time out it just hangs" 来自
microsoft.public.win32.programmer.kernel
邮件列表,1997
Q) Does anyone know of a way to get LockFileEx to time out ?
...
A) you can only have it fail imemdiately, sleep, and loop back
until you reach some retry limit.
flock()
是 PHP 的 可移植 咨询文件锁定功能。他们明确宣传它甚至可以在 windows:
flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows).
我想在阻塞 flock()
上也设置一个 可移植 超时(并且不用忙等待 LOCK_NB
选项)。在 UNIX 中,这可以简单地通过设置一个将发送 SIGALRM
:
pcntl_signal(SIGALRM, function() {});
pcntl_alarm(3);
try {
if (!flock($handle, LOCK_EX)) {
throw new \Exception("Timeout");
}
} finally {
pcntl_alarm(0);
pcntl_signal_dispatch();
pcntl_signal(SIGALRM, SIG_DFL);
}
是否有一种可移植的方法来为阻塞设置超时 flock()
?如果是,怎么做?
我认为在 Windows 上没有任何方法可以在没有繁忙的等待/轮询循环的情况下执行此操作。
PHP 实现 flock
on windows using LockFileEx
(see flock_compat.c:132
)。从这些类似的问题中可以看出,无法在 LockFileEx
上设置超时或取消等待 LockFileEx
请求的进程(即没有等同于 SIGALRM
此用例的信号):
- LockFile with timeout?(2011 年问)
Q) If I want to wait for file-lock with timeout, how would I go about it?
...
A) write a small loop to check the return code
- "LockFileEx can't time out it just hangs" 来自
microsoft.public.win32.programmer.kernel
邮件列表,1997
Q) Does anyone know of a way to get LockFileEx to time out ?
...
A) you can only have it fail imemdiately, sleep, and loop back until you reach some retry limit.