Simple file lock, times out after 20 second assuming lock is stale

def _simple_lock(f):
    """Simple file lock, times out after 20 second assuming lock is stale
    """
    lock_file = f + ".lock"
    timeout = 20
    curtime = 0
    interval = 2
    while os.path.exists(lock_file):
        time.sleep(interval)
        curtime += interval
        if curtime > timeout:
            os.remove(lock_file)
    with open(lock_file, "w") as out_handle:
        out_handle.write("locked")
    yield
    if os.path.exists(lock_file):
        os.remove(lock_file)