如何在大型 php 应用程序中写入文件(多个问题)
在大型 php 应用程序中写入文件的最佳方法是什么?假设每秒需要大量写入。最好的方法是如何解决这个问题。
我可以打开文件并附加数据吗?或者我应该打开、锁定、写入和解锁。
文件将发生什么,需要写入其他数据。此活动会丢失,还是会被保存。如果将其保存,将停止应用程序。
如果您去过,感谢您的阅读!
What is the best way to write to files in a large php application. Lets say there are lots of writes needed per second. How is the best way to go about this.
Could I just open the file and append the data. Or should i open, lock, write and unlock.
What will happen of the file is worked on and other data needs to be written. Will this activity be lost, or will this be saved. and if this will be saved will is halt the application.
If you have been, thank you for reading!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这里有一个简单的例子,强调了同时写入的危险:
如果追加是安全的,您应该得到一个包含 100 行的文件,所有这些行大约有 10,000 个字符长,并且以整数开头。有时,当您运行此脚本时,这正是您将得到的结果。有时,一些附加会发生冲突,并且它会被破坏。
您可以使用
grep '^[^0-9]' test.txt
找到损坏的行,这是因为 文件附加仅在以下情况下是原子的:
如果您在日志追加过程中对 fwrite 进行了多次调用,或者您写入的内容超过 4k,那么所有的赌注都会失败。
现在,至于这是否重要:您是否同意在重负载下日志中出现一些损坏的行?老实说,大多数时候这是完全可以接受的,并且您可以避免文件锁定的开销。
Here's a simple example that highlights the danger of simultaneous wites:
If appends were safe, you should get a file with 100 lines, all of which roughly 10,000 chars long, and beginning with an integer. And sometimes, when you run this script, that's exactly what you'll get. Sometimes, a few appends will conflict, and it'll get mangled, however.
You can find corrupted lines with
grep '^[^0-9]' test.txt
This is because file append is only atomic if:
If you make more than a single call to fwrite during your log append, or you write more than about 4k, all bets are off.
Now, as to whether or not this matters: are you okay with having a few corrupt lines in your log under heavy load? Honestly, most of the time this is perfectly acceptable, and you can avoid the overhead of file locking.
我确实有高性能、多线程应用程序,其中所有线程都写入(附加)到单个日志文件。到目前为止,没有注意到任何问题,每个线程每秒写入多次,并且不会丢失任何内容。我认为仅附加到大文件应该没有问题。但是如果你想修改已经存在的内容,特别是并发性 - 我会选择锁定,否则可能会发生大混乱......
I do have high-performance, multi-threaded application, where all threads are writing (appending) to single log file. So-far did not notice any problems with that, each thread writes multiple times per second and nothing gets lost. I think just appending to huge file should be no issue. But if you want to modify already existing content, especially with concurrency - I would go with locking, otherwise big mess can happen...
如果并发是一个问题,那么您确实应该使用数据库。
If concurrency is an issue, you should really be using databases.
如果您只是编写日志,也许您必须查看 syslog 函数,因为 syslog提供了一个api。
您还应该将写入委托给专用后端并以异步方式完成这项工作?
If you're just writing logs, maybe you have to take a look in syslog function, since syslog provides an api.
You should also delegate writes to a dedicated backend and do the job in an asynchroneous maneer ?
这些是我的2p。
除非出于特定原因需要一个唯一的文件,否则我会避免将所有内容附加到一个大文件中。相反,我会按时间和维度包装文件。可以为此定义几个配置参数(wrap_time 和wrap_size)。
另外,我可能会引入一些缓冲以避免等待写入操作完成。
也许 PHP 不是最适合此类操作的语言,但它仍然是可能的。
These are my 2p.
Unless a unique file is needed for a specific reason, I would avoid appending everything to a huge file. Instead, I would wrap the file by time and dimension. A couple of configuration parameters (wrap_time and wrap_size) could be defined for this.
Also, I would probably introduce some buffering to avoid waiting the write operation to be completed.
Probably PHP is not the most adapted language for this kind of operations, but it could still be possible.
使用 flock()
请参阅此 问题
Use flock()
See this question
如果您只需要附加数据,PHP 应该可以满足,因为文件系统应该处理同时附加的问题。
If you just need to append data, PHP should be fine with that as filesystem should take care of simultaneous appends.