Safely (i.e. with locking) append a line to
    the given file, serialized as JSON.

def append_line(filename, **line):
    """Safely (i.e. with locking) append a line to
    the given file, serialized as JSON.
    """
    global lock

    data = json.dumps(line, separators=(',', ':')) + '\n'
    with lock:
        with file(filename, 'a') as fp:
            fp.seek(0, SEEK_END)
            fp.write(data)