Single-instance daemons

Дамјан Георгиевски gdamjan at gmail.com
Mon Nov 17 00:15:52 EST 2008



> As per Stevens/Rago, "file and record locking provides a convenient
> mutual-exclusion mechanism".  

On linux (at least) there's one nice trick to get a single-instance
program. Create a unix domain socket, and bind it to an address that
begins with the null character '\0'. You can bind the same address a
second time, and if the process dies the socket is automatically
destroyed. It will not leave anything on the filesystem.

def single_instance(id):
    import socket
    s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    s.bind('\0' + id)
    return s


-- 
дамјан ( http://softver.org.mk/damjan/ )

When you do things right, people won't be sure if you did anything at all.



More information about the Python-list mailing list