问题
The sync man page says:
sync() causes all buffered modifications to file metadata and data to be written to the underlying file systems.
Does Python have a call to do this?
P.S. Not fsync, I see that.
回答1:
Python 3.3 has os.sync, see the docs. The source confirms it is the same thing.
For Python 2 you might have to make an external call to the system.
回答2:
As said, Python 3.3 has the call - on Python 2.x, since it is a simple system call, requiring no data to be passed back and forth, you can use ctypes to make the call:
>>> import ctypes
>>> libc = ctypes.CDLL("libc.so.6")
>>> libc.sync()
0
回答3:
Combining the two answers, I use the following at the top of my module:
if hasattr(os, 'sync'):
sync = os.sync
else:
import ctypes
libc = ctypes.CDLL("libc.so.6")
def sync():
libc.sync()
来源:https://stackoverflow.com/questions/15983272/does-python-have-sync