Does the file write to disk with Python open (). Does Write () ensure data is available to other processes?

One Python process writes status updates to a file to read other processes. In some cases, status updates are performed repeatedly and quickly in a loop. The easiest and fastest approach is to use to open (). Write () on one line:

open(statusfile,'w').write(status) 

An alternative four-line approach that forces data to disk. This greatly reduces performance:

 f = open(self.statusfile,'w') f.write(status) os.fsync(f) f.close() 

I am not trying to protect against an OS crash. So the approach forces the data into the OS buffer so that other processes read the latest status data when they open the file from disk? Or do I need to use os.fsync ()?

+6
source share
2 answers

No, the first approach does not guarantee that the data will be written out, since it is not guaranteed that the file will be cleaned and closed after the handle no longer refers to its write member. This is most likely due to CPython, but not necessarily to other Python interpreters; this is a Python garbage collector implementation detail.

You really should use the second approach, except that os.fsync not required; just close the file and the data should be accessible to other processes.

Or, even better ( Python> = 2.5 ):

 with open(self.statusfile, 'w') as f: f.write(status) 

The with version is safe for exceptions: the file is closed even if write does not work.

+10
source

With Python 2.2, it was possible to subclass built-in language types. This means that you can get your own file type, the write() method of which returned self instead of anything like the built-in version. This will also allow you to bind a call to the close() method at the end of your single-line file.

 class ChainableFile(file): def __init__(self, *args, **kwargs): return file.__init__(self, *args, **kwargs) def write(self, str): file.write(self, str) return self def OpenFile(filename, *args, **kwargs): return ChainableFile(filename, *args, **kwargs) statusfile = 'statusfile.txt' status = 'OK\n' OpenFile(statusfile,'w').write(status).close() 
0
source

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


All Articles