def maybe_run()

in asfpy/justone.py [0:0]


def maybe_run(fifo_fname, func, stale=STALE):
    try:
        info = os.stat(fifo_fname)
    except OSError:
        # Some error (assume it doesn't exist), so run the func
        return _run_func(fifo_fname, func)

    # Opening a FIFO in non-blocking will throw ENXIO if no reader
    # is on the other end.
    try:
        fd = os.open(fifo_fname, os.O_WRONLY | os.O_NONBLOCK)

        # Successful open means a reader exists. Thus, we're done.
        os.close(fd)
        return DID_NOT_RUN

    except OSError as e:
        if e.errno == errno.ENOENT:
            # RACE: the FIFO just disappeared. Meaning another
            # process *just* completed. Go ahead and run the func.
            return _run_func(fifo_fname, func)

        # Note: ENXIO means there is no reader on the other end.
        # Bail on anything else, as we can't handle it.
        if e.errno != errno.ENXIO:
            raise

        # Check for a stale FIFO.

        # There is no reader process, but the FIFO exists. Could be
        # a race condition (FIFO is opening/closing in the reader),
        # or maybe a stale FIFO.
        if time.time() - info.st_mtime > stale:
            ### RACE: we might be removing another runner's FIFO
            os.unlink(fifo_fname)
            return _run_func(fifo_fname, func)

        # Not stale (yet). Don't do anything for now.
        return DID_NOT_RUN