Exemplo n.º 1
0
def progress_startup(pid: typing.Optional[int] = None, fd: typing.Optional[int] = None):
    # This can be called multiple times, if it desired to monitor several
    # input files in sequence.
    #
    # If pid is None, the cirrent process will be monitored.  If pid is not
    # None, the specified process will be monitored.
    #
    # If target_fd is None, them all fds will be monitored.  If target is not
    # None, the specific fd will be monitored: it must already be open, and it
    # should be an input file.  There is no option to moitor multiple specific
    # input files other than calling this routine sequentially
    import os
    import sh
    global _save_progress, _save_progress_tty
    if _save_progress and _save_progress_tty is not None:
        global _save_progress_command
        if _save_progress_command is not None:
            # Shut down an existing process monitor.
            try:
                _save_progress_command.terminate()
            except Exception:
                pass
            _save_progress_command = None

        # Start a process monitor.
        if pid is None:
            pid = os.getpid()
        if fd is None:
            _save_progress_command = sh.pv("-d {}".format(pid), _out=_save_progress_tty, _err=_save_progress_tty, _bg=True)
        else:
            _save_progress_command = sh.pv("-d {}:{}".format(pid, fd),
                                           _out=_save_progress_tty, _err=_save_progress_tty, _bg=True)
Exemplo n.º 2
0
def progress_startup(pid: typing.Optional[int] = None,
                     fd: typing.Optional[int] = None):
    # This can be called multiple times, if it desired to monitor several
    # input files in sequence.
    #
    # If pid is None, the current process will be monitored.  If pid is not
    # None, the specified process will be monitored.
    #
    # If target_fd is None, them all fds will be monitored.  If target is not
    # None, the specific fd will be monitored: it must already be open, and it
    # should be an input file.  There is no option to moitor multiple specific
    # input files other than calling this routine sequentially
    #
    # TODO: use the envar KGTK_PV_COMMAND to get the `pv` command.
    global _save_progress, _save_progress_tty, _save_progress_debug
    if _save_progress and _save_progress_tty is not None:
        global _save_progress_command
        if _save_progress_command is not None:
            # Shut down an existing process monitor.
            try:
                _save_progress_command.terminate()
            except Exception:
                pass
            _save_progress_command = None

        # Give up if cannot find `pv`:
        if shutil.which('pv') is None:
            if _save_progress_debug:
                print("progress_startup: cannot find pv.",
                      file=sys.stderr,
                      flush=True)
            return

        # Start a process monitor.
        if pid is None:
            pid = os.getpid()
        try:
            if fd is None:
                if _save_progress_debug:
                    print("progress_startup: starting pv with pid %d" % pid,
                          file=sys.stderr,
                          flush=True)
                _save_progress_command = sh.pv("-d {}".format(pid),
                                               _out=_save_progress_tty,
                                               _err=_save_progress_tty,
                                               _bg=True)
            else:
                if _save_progress_debug:
                    print("progress_startup: starting pv with pid %d fd %d" %
                          (pid, fd),
                          file=sys.stderr,
                          flush=True)
                _save_progress_command = sh.pv("-d {}:{}".format(pid, fd),
                                               _out=_save_progress_tty,
                                               _err=_save_progress_tty,
                                               _bg=True)
        except Exception as e:
            # Ignore the exception unless _save_progress_debug is True.
            if _save_progress_debug:
                print("progress_startup: %s" % str(e),
                      file=sys.stderr,
                      flush=True)
                raise