system wide mutex

Marko Rauhamaa marko at pacujo.net
Sun Feb 9 11:14:44 EST 2014


Asaf Las <roegltd at gmail.com>:

> Which one is most recommended to use for mutex alike locking to 
> achieve atomic access to single resource:
>
> - fcntl.lockf

I recommend fcntl.flock:

========================================================================
#!/usr/bin/python3

import sys, fcntl, time

with open("test.lock", "w+") as lock:
    fcntl.flock(lock.fileno(), fcntl.LOCK_EX)
    # begin critical
    sys.stdout.write("hello\n")
    time.sleep(3)
    sys.stdout.write("world\n")
    # end critical
========================================================================


Marko



More information about the Python-list mailing list