我应该保持文件打开状态还是应该经常打开和关闭文件? [英] Should I keep a file open or should I open and close often?

查看:158
本文介绍了我应该保持文件打开状态还是应该经常打开和关闭文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,习惯只打开一次文件吗?

#!/usr/bin/env perl
use warnings;
use 5.012;
use autodie;

my $file = 'my_file';

open my $fh, '>>', $file;
say $fh "Begin";
close $fh;

$SIG{INT} = sub { 
    open my $fh, '>>', $file; 
    say $fh "End";
    close $fh;
    exit 
};

my $result;
while ( 1 ) {
    $result++;
    # ...
    # ...
    # ...
    open my $fh, '>>', $file; 
    say $fh $result;
    close $fh;
    sleep 3;
}

解决方案

简短答案:几乎总是应该只打开/关闭一次.下面的详细信息. >

是否这样做的决定取决于4件事:

  1. 是否可能需要其他处理来写入文件?

    如果是这样,则您可能需要锁定文件,而为同时使用而设计的进程的良好行为是尽可能快地释放锁定的共享资源,以便其他人可以获得锁定.

  2. 是否需要打开许多文件?

    如果是这样,则打开的文件过多可能会用完文件句柄,因此需要关闭.

  3. 如果程序崩溃,您对文件中的数据丢失有多大的容忍度.

    如果需要将缓冲区中的数据保存到文件中,则需要刷新它.可以通过频繁关闭来完成,尽管更好的解决方案是在打开的文件句柄上进行频繁刷新或自动刷新.

  4. 您是否非常在意磁盘空间不足后无法关闭文件?

    如果是这样,关闭/重新打开文件的次数越多,由于文件系统已满而丢失的数据就越少,因此自上次open以来的所有写操作都将消失.

在任何其他情况下,只需打开/关闭一次(好吧,也许在__DIE__处理程序和END{}块中额外关闭(大多数情况下,您可能会在其他情况下).

这是因为打开/关闭文件只会无故浪费系统资源,并且会使您的代码更长.更具体地说,文件打开和关闭是昂贵的操作,需要系统调用(这可能迫使从用户区跳转到内核)和额外的磁盘IO,这在资源上非常昂贵.要验证这一点,请在您的OS上运行一些系统利用率测量实用程序,然后运行一个Perl脚本,该脚本除了打开/关闭10000个不同的文件名(每次100次)外,什么也不做.

请注意(关于场景#3/#4),如果您非常关心不丢失任何数据,您首先不应该使用文件IO -使用数据库或消息传递具有交付保证的系统.

Is it customary in such a case to open the file only once?

#!/usr/bin/env perl
use warnings;
use 5.012;
use autodie;

my $file = 'my_file';

open my $fh, '>>', $file;
say $fh "Begin";
close $fh;

$SIG{INT} = sub { 
    open my $fh, '>>', $file; 
    say $fh "End";
    close $fh;
    exit 
};

my $result;
while ( 1 ) {
    $result++;
    # ...
    # ...
    # ...
    open my $fh, '>>', $file; 
    say $fh $result;
    close $fh;
    sleep 3;
}

解决方案

Short answer: Almost always, you should open/close only once. Details below.

The decision of whether to do that depends on 4 things:

  1. Are there other processes that may need to write to the file?

    If so, you may need to lock the file, and the good behavior for a process designed for concurrent use is to release the locked shared resource as fast as possible so others can get the lock.

  2. Are there MANY files you need to open?

    If so, you might run out of file handles with too many open files, so you need to close.

  3. How much tolerance you have for losing data in the file if the program crashes.

    If you need to save the data from buffer into the file, you need to flush it. This CAN be done by frequently closing, although a better solution is either frequent flushing or autoflush on the file handle being turned on.

  4. Do you care greatly about not being able to close the file after running out of disk space?

    If so, the more often you close/reopen the file, the less data you will lose due to filesystem being full so whatever you wrote since last open is gone.

In any other scenario, only open/close once (well, plus maybe extra close in __DIE__ handler and END{} block (and majority of time, you WILL likely be in other scenarios).

That is because opening/closing the file just wastes system resources for no reason whatsoever AND makes your code longer. To be more specific, file open and close are expensive operations, requiring both system calls (that may force jump to kernel from userland), and extra disk IO which is VERY expensive resource wise. To verify that, run some system utilization measurment utility on your OS, and run a Perl script that does nothing except open/close 10000 different file names 100 times each.

Please note (regarding scenarios #3/#4) that if you care greatly about not losing any data, you should not be using file IO in the first place - use a database or a messaging system with delivery guarantees.

这篇关于我应该保持文件打开状态还是应该经常打开和关闭文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆