Ruby open-uri 文件锁定
我遇到一个问题,下载到本地文件会锁定它,直到我杀死 ruby 脚本。 凭直觉,我改变了调用的形式,现在它不会锁定文件。
有人能告诉我为什么下面的第二种形式不锁定文件,而第一种形式却锁定文件?
此表单锁定本地文件:
open(ver_local_zip, "w+").write(open(remote_zip, :proxy=>ftp_proxy).read)
此表单不锁定文件:
open(ver_local_zip, "w+") { |f| f.write(open(remote_zip, :proxy=>ftp_proxy).read) }
谢谢。
I had an issue where downloading to a local file was locking it until I killed the ruby script. On a hunch, I changed the form of the call and now it doesn't lock the file.
Can someone tell me why the second form below doesn't lock the file but the first one does?
This form locks the local file:
open(ver_local_zip, "w+").write(open(remote_zip, :proxy=>ftp_proxy).read)
This form doesn't lock the file:
open(ver_local_zip, "w+") { |f| f.write(open(remote_zip, :proxy=>ftp_proxy).read) }
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用块调用 open 方法通常会在块执行完成后立即关闭文件。 如果没有块,则 open 返回一个文件(句柄),该文件将在下一次垃圾收集发生时释放。
Calling the method open with a block usually closes the file immediately after the execution of the block finished. Without a block, open return a file (handle) that will be freed when the next garbage collection takes place.