def write_pid(filename): '''Atomically writes a pid file, or fails if the file already exists.''' fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o644) os.write(fd, str(os.getpid())) os.close(fd)