Why is “slurping” a file not a good practice?

后端 未结 3 1839
面向向阳花
面向向阳花 2020-11-22 04:22

Why is \"slurping\" a file not a good practice for normal text-file I/O, and when is it useful?

For example, why shouldn\'t I use these?

File.read(\'         


        
3条回答
  •  面向向阳花
    2020-11-22 05:07

    This is kind of old, but I'm a little surprised that no one makes a mention that slurping an input file makes the program practically useless for pipelines. In a pipeline, the input file might be small but slow. If your program is slurping that means it's not working with the data as it becomes available and rather makes you wait for however long it might take for the input to complete. How long? It could be anything, like hours or days, more or less, if I'm doing a grep or find in a big hierarchy. It could also be designed to not complete, like an infinite file. For example, journalctl -f will continue to output whatever events happen in the system without stopping; tshark will output whatever it sees going on in the network without stopping; ping will continue pinging without stopping. /dev/zero is infinite, /dev/urandom is infinite.

    The only time I could see slurping as acceptable would maybe be in configuration files, since the program is probably not able to do anything anyway until it finishes reading that.

提交回复
热议问题