读写文件的并发性
我有一个网站,我的网站的一部分需要在服务器上读取/写入纯文本文件。大多数时候,我们有 50 到 100 个并发在线用户,他们在我们网站上的操作会导致读/写某些文件,其中该文件是某些其他页面的临时占位符。
所以我想了解并发读/写冲突,以及如果发生这种情况,避免这种情况的最佳实践是什么。我的服务器如何管理这些请求?
I have a website and part of my website needs to read/write a plain text file on server. most of the time we have between 50 to 100 concurrent online users and their actions on our site result to read/write certain file in which that is a temporary place holder for some other pages.
So I want to know about concurrent read/write conflicts and what's the best practice to avoid that if that happened. How does my server manage these requests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
比网络服务器更重要的是底层操作系统和文件系统。
您可以(在受支持的情况下)使用 flock() 导致所有其他对独占锁的尝试阻塞并堆栈在其后面。
Of more importance than the webserver is the underlying OS and filesystem.
You can (in supported circumstances) obtain an exclusive lock on a file handle in PHP using flock() causing all other attempts at an exclusive lock to block and stack behind it.
*nix 系统中的一个选项是创建一个临时文件,写入它,对其进行 fsync() 处理,然后将其重命名 () 到目标文件名。当然,这仅适用于单个作家,或者如果您只想要最新的作家。
An option in *nix systems is creating a temporary file, writing to it, fsync()ing it, and then rename()ing to the target filename. Of course this only works for a single writer, or if you only want the latest writer.