Does Python have sync?

柔情痞子 提交于 2019-12-08 14:46:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!