Abort slow stream to disk after burning?

Is there a way to interrupt the operation of writing python in such a way that the OS does not consider it necessary to clear unwritten data on the disk?

I write data to a USB device, usually a lot of megabytes. I use 4096 bytes as the block size for writing, but it seems that Linux collects a bunch of data at an early stage and writes it slowly to a USB device. If at some point during recording my user decides to cancel, I want the application to simply stop writing right away. I see that there is a delay between when the data stops coming from the application, and the USB activity indicator stops blinking. A few seconds, usually up to 10 seconds. I found that the application is holding the close () method, I suppose while waiting for the OS to finish writing buffered data. I call flush () after each write, but this does not seem to affect the delay. I looked at the python docs for an answer, but didn't find anything.

+3
source share
3 answers

It is somewhat dependent on the file system, but on some file systems, if you delete a file before (all), it stands out, IO for writing blocks will never happen. It may also be true if you truncate it so that the part that is still being recorded is chopped off.

Not sure if you really can interrupt the recording if you want to access the data. Also, the types of file systems that support this (e.g. xfs, ext4) are usually not used on USB sticks.

If you want to flush data to disk, use fdatasync (). Simply flushing the IO library buffer on the OS will not result in physical flushing.

+2
source

, , "" . , ctype pokery. , OSX, , Linux:

f = open('flibble1.txt', 'w')
f.write("hello world")
import ctypes
x = ctypes.cdll.LoadLibrary('/usr/lib/libc.dylib')
x.close(f.fileno())
try:
  del f
catch IOError:
  pass

/usr/lib/libc.dylib libc.so.6 /usr/lib Linux, . , close() fclose(), fsync() , .

, .

+2

When you interrupt a write operation, try to do it file.truncate(0);before closing it.

+1
source

Source: https://habr.com/ru/post/1737243/


All Articles