Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python have sync?

Tags:

python

linux

sync

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.

like image 279
dfrankow Avatar asked Apr 13 '13 01:04

dfrankow


People also ask

Does Python sync or async?

There are two basic types of methods in the Parallels Python API: synchronous and asynchronous. When a synchronous method is invoked, it completes executing before returning to the caller. An asynchronous method starts a job in the background and returns to the caller immediately.

Does Python run synchronously?

That first part is the most critical thing to understand - Python code can now basically run in one of two "worlds", either synchronous or asynchronous. You should think of them as relatively separate, having different libraries and calling styles but sharing variables and syntax.

Is Python synchronous by default?

It's the language, in python most apis are synchronous by default, and the async version is usually an "advanced" topic.

Does Python have async?

It makes use of Python async features using asyncio/await provided in Python 3. The time and queue modules have been replaced with the asyncio package. This gives your program access to asynchronous friendly (non-blocking) sleep and queue functionality.


3 Answers

Python 3.3 has os.sync, see the docs. The source confirms it is the same thing.

For Python 2 you can to make an external call to the system:

from subprocess import check_call
check_call(['sync'])
like image 132
bbayles Avatar answered Sep 30 '22 23:09

bbayles


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
like image 36
jsbueno Avatar answered Sep 30 '22 22:09

jsbueno


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()
like image 28
Frederick Nord Avatar answered Sep 30 '22 22:09

Frederick Nord