在 PHP 中,打开本地文件时如何让 fopen 很快超时?

发布于 2024-09-24 15:06:02 字数 446 浏览 3 评论 0原文

我在使用 PHP 时遇到问题,我经常写入文件,有时需要很长时间才能打开该文件。 完整的描述在这里:

fopen 文件锁定 PHP (读取器/写入器类型的情况)

我的问题是如何让 fopen 在 50 毫秒内超时。 我查看了 stream-context-create 但这似乎适用于 HTTP,或者至少,如果它适用于本地文件,我不确定如何在数组中指定该选项。

有什么想法吗?

谢谢!
丹尼尔

I'm having a problem with PHP where i'm writing to a file very often, and sometimes it takes a long time to open that file.
The complete description is here:

fopen file locking in PHP (reader/writer type of situation)

My question is how can I get fopen to timeout in, say, 50ms.
I looked at stream-context-create but that seems to be for HTTP, or at least, if it'll work for local files, I'm not sure how to specify the option in the array.

Any ideas?

Thank you!
Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-10-01 15:06:02

我不确定您在这里尝试什么,但在某些平台(但不是 Windows)中,您可以使用 n 标志以非阻塞模式打开文件:

$f = fopen("/tmp/foo/bar", "wn+");

这应该立即返回。然后您可以使用 stream_select 超时时间为 50 毫秒。

我说“可能”是因为这个标志没有记录。

I'm not sure what you're trying here, but in some platforms (not Windows, though), you can open a file in non-blocking mode with the n flag:

$f = fopen("/tmp/foo/bar", "wn+");

This should return immediately. You can then probably use stream_select with a timeout of 50 ms.

I say "probably" because this flag is not documented.

椵侞 2024-10-01 15:06:02

php.ini 中的 default_socket_timeout 变量更改为 '1',会有帮助吗?

changing the default_socket_timeout variable in the php.ini to '1', would that helps?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文