Profile a Python script continuously.
763874
def live_profile(script, argv, profiler_factory, interval, spawn, signum,
                 pickle_protocol, mono):
    """"""
    filename, code, globals_ = script
    sys.argv[:] = [filename] + list(argv)
    parent_sock, child_sock = socket.socketpair()
    stderr_r_fd, stderr_w_fd = os.pipe()
    pid = os.fork()
    if pid:
        # parent
        os.close(stderr_w_fd)
        viewer, loop = make_viewer(mono)
        # loop.screen._term_output_file = open(os.devnull, 'w')
        title = get_title(filename)
        client = ProfilingClient(viewer, loop.event_loop, parent_sock, title)
        client.start()
        try:
            loop.run()
        except KeyboardInterrupt:
            os.kill(pid, signal.SIGINT)
        except BaseException:
            # unexpected profiler error.
            os.kill(pid, signal.SIGTERM)
            raise
        finally:
            parent_sock.close()
        # get exit code of child.
        w_pid, status = os.waitpid(pid, os.WNOHANG)
        if w_pid == 0:
            os.kill(pid, signal.SIGTERM)
        exit_code = os.WEXITSTATUS(status)
        # print stderr of child.
        with os.fdopen(stderr_r_fd, 'r') as f:
            child_stderr = f.read()
        if child_stderr:
            sys.stdout.flush()
            sys.stderr.write(child_stderr)
        # exit with exit code of child.
        sys.exit(exit_code)
    else:
        # child
        os.close(stderr_r_fd)
        # mute stdin, stdout.
        devnull = os.open(os.devnull, os.O_RDWR)
        for f in [sys.stdin, sys.stdout]:
            os.dup2(devnull, f.fileno())
        # redirect stderr to parent.
        os.dup2(stderr_w_fd, sys.stderr.fileno())
        frame = sys._getframe()
        profiler = profiler_factory(base_frame=frame, base_code=code)
        profiler_trigger = BackgroundProfiler(profiler, signum)
        profiler_trigger.prepare()
        server_args = (interval, noop, pickle_protocol)
        server = SelectProfilingServer(None, profiler_trigger, *server_args)
        server.clients.add(child_sock)
        spawn(server.connected, child_sock)
        try:
            exec_(code, globals_)
        finally:
            os.close(stderr_w_fd)
            child_sock.shutdown(socket.SHUT_WR)
1139313
def handler_profiler(filename=None):
    
    if filename == None:
        f = sys.stdout
    else:
        f = open(filename, 'w')
    def _callfunc(handle):
        @wraps(handle)
        def func(self, topic, msg):
            t0 = time.time()
            handle(self, topic, msg)
            t1 = time.time()
            print(f'{self.name}-handle运行时间:{t1 - t0}s', file=f)

        return func
    return _callfunc
423876
def _cron_profile():
    
    #The main ingredients of the file are the import of the virtualenvwrapper
    #and the setting of the PYCI_XML variable for global configuration.
    from os import path
    cronpath = path.expanduser("~/.cron_profile")
    if not path.isfile(cronpath):
        from os import getenv
        xmlpath = getenv("PYCI_XML")    
        contents = ['source /usr/local/bin/virtualenvwrapper.sh',
                    'export PYCI_XML="{}"'.format(xmlpath)]
        with open(cronpath, 'w') as f:
            f.write('\n'.join(contents))
638514
function monitor() {
    if(!child.pid) {
      // If the number of periodic starts exceeds the max, kill the process
      if (starts >= options.maxRetries) {
        if ((Date.now() - startTime) > maxMilliseconds) {
          console.error(
            `Too many restarts within the last ${maxMilliseconds / 1000} seconds. Please check the script.`
          )
          process.exit(1)
        }
      }

      setTimeout(() => {
        wait = wait * options.growPercentage
        attempts += 1;
        if (attempts > options.maxRestarts && options.maxRestarts >= 0){
          console.error(
            `${options.name} will not be restarted because the maximum number of total restarts has been exceeded.`
          )
          process.exit()
        } else {
          launch()
        }
      }, wait)
    } else {
      attempts = 0
      wait = options.restartDelay * 1000
    }
  }
25602
def profile(self, frame, event, arg): #PYCHOK arg requ. to match signature
        """"""
        if (self.events == None) or (event in self.events):
            frame_info = inspect.getframeinfo(frame)
            cp = (frame_info[0], frame_info[2], frame_info[1])
            if self.codepoint_included(cp):
                objects = muppy.get_objects()
                size = muppy.get_size(objects)
                if cp not in self.memories:
                    self.memories[cp] = [0,0,0,0]
                    self.memories[cp][0] = 1
                    self.memories[cp][1] = size
                    self.memories[cp][2] = size
                else:
                    self.memories[cp][0] += 1
                    if self.memories[cp][1] > size:
                        self.memories[cp][1] = size
                    if self.memories[cp][2] < size:
                        self.memories[cp][2] = size
151551
def _StopProfiling(self):
    """"""
    if self._guppy_memory_profiler:
      self._guppy_memory_profiler.Sample()
      self._guppy_memory_profiler.Stop()
      self._guppy_memory_profiler = None

    if self._memory_profiler:
      self._memory_profiler.Stop()
      self._memory_profiler = None

    if self._processing_profiler:
      self._processing_profiler.Stop()
      self._processing_profiler = None

    if self._serializers_profiler:
      self._serializers_profiler.Stop()
      self._serializers_profiler = None

    if self._storage_profiler:
      self._storage_profiler.Stop()
      self._storage_profiler = None

    if self._tasks_profiler:
      self._tasks_profiler.Stop()
      self._tasks_profiler = None
62178
def default_profiler(f:Frame, _type:str, _value:Any):
    ''' 
    '''
    try:
        profile_print(
            '\n'.join([
                get_frame_src(f),
                get_locals(f),
                '----------------------------------------'
            ])
        )
    except:
        pass
1175630
def profile(func):
    
    @inlineCallbacks
    def callme(*args, **kwargs):
        start = time.time()
        ret = yield func(*args, **kwargs)
        time_to_execute = time.time() - start
        log.msg('%s executed in %.3f seconds' % (func.__name__, time_to_execute))
        returnValue(ret)
    return callme
310151
def profileit(func):
    
    def wrapper(*args, **kwargs):
        datafn = func.__name__ + ".profile" # Name the data file sensibly
        prof = cProfile.Profile()
        prof.enable()
        retval = prof.runcall(func, *args, **kwargs)
        prof.disable()
        stats = pstats.Stats(prof)
        try:
            stats.sort_stats('cumtime').print_stats()
        except KeyError:
            pass  # breaks in python 2.6
        return retval

    return wrapper
1294847
def run(self):
        """"""
        last_status = ''
        # lambda is used to avoid http://bugs.python.org/issue30473 in py36
        start_times = defaultdict(lambda: datetime.datetime.now())
        max_stage_id = -1

        status = self.sc.statusTracker()
        while True:
            with self.condition:
                if self.sc._jsc is None or not self.alive:
                    # End the thread
                    self.paused = True
                    break
                elif self.paused:
                    # Pause the thread
                    self.condition.wait()
            stage_ids = status.getActiveStageIds()
            progressbar_list = []
            # Only show first 3
            stage_counter = 0
            current_max_stage = max_stage_id
            for stage_id in stage_ids:
                stage_info = status.getStageInfo(stage_id)
                if stage_info and stage_info.numTasks > 0:
                    # Set state variables used for flushing later
                    current_max_stage = stage_id
                    stage_counter += 1
                    td = datetime.datetime.now() - start_times[stage_id]
                    s = _format_stage_info(self.bar_width, stage_info, td, self.timedelta_formatter)
                    progressbar_list.append(s)
                if stage_counter == 3:
                    break

            # Ensure that when we get a new maximum stage id we print a \n
            # to make the progress bar go on to the next line.
            if current_max_stage > max_stage_id:
                if last_status != '':
                    sys.stderr.write("\n")
                sys.stderr.flush()
                max_stage_id = current_max_stage

            new_status = ' '.join(progressbar_list)
            if new_status != last_status:
                sys.stderr.write("\r" + new_status)
                sys.stderr.flush()
                last_status = new_status
            time.sleep(self.sleep_time)
822718
def Main():
  
  argument_parser = argparse.ArgumentParser(description=(
      'Plots memory usage from profiling data.'))

  argument_parser.add_argument(
      '--output', dest='output_file', type=str, help=(
          'path of the output file to write the graph to instead of using '
          'interactive mode. The output format deduced from the extension '
          'of the filename.'))

  argument_parser.add_argument(
      'profile_path', type=str, help=(
          'path to the directory containing the profiling data.'))

  options = argument_parser.parse_args()

  if not os.path.isdir(options.profile_path):
    print('No such directory: {0:s}'.format(options.profile_path))
    return False

  names = ['time', 'queued', 'processing', 'to_merge', 'abandoned', 'total']

  glob_expression = os.path.join(options.profile_path, 'task_queue-*.csv.gz')
  for csv_file_name in glob.glob(glob_expression):
    data = numpy.genfromtxt(
        csv_file_name, delimiter='\t', dtype=None, encoding='utf-8',
        names=names, skip_header=1)

    pyplot.plot(data['time'], data['queued'], label='queued')
    pyplot.plot(data['time'], data['processing'], label='processing')
    pyplot.plot(data['time'], data['to_merge'], label='to merge')
    pyplot.plot(data['time'], data['abandoned'], label='abandoned')

  pyplot.title('Number of tasks over time')

  pyplot.xlabel('Time')
  pyplot.xscale('linear')

  pyplot.ylabel('Number of tasks')
  pyplot.yscale('linear')

  pyplot.legend()

  if options.output_file:
    pyplot.savefig(options.output_file)
  else:
    pyplot.show()

  return True
596761
async def run_script(self, script):
        """"""
        script = os.linesep.join(['set -x', 'set +B', script, 'exit']) + os.linesep
        self.proc.stdin.write(script.encode('utf-8'))
        await self.proc.stdin.drain()
        self.proc.stdin.close()
1097866
def serve_forever(self):
        """"""

        # Test if client and server are in the same major version
        if not self.login():
            logger.critical("The server version is not compatible with the client")
            self.end()
            return self.client_mode

        exitkey = False
        try:
            while True and not exitkey:
                # Update the stats
                cs_status = self.update()

                # Update the screen
                if not self.quiet:
                    exitkey = self.screen.update(self.stats,
                                                 cs_status=cs_status,
                                                 return_to_browser=self.return_to_browser)

                # Export stats using export modules
                self.stats.export(self.stats)
        except Exception as e:
            logger.critical(e)
            self.end()

        return self.client_mode
517968
def watch(directory=None, auto_clear=False, extensions=[]):
    """"""
    if directory and not os.path.isdir(directory):
        raise ValueError('Directory not found: ' + directory)
    directory = os.path.abspath(directory)

    # Initial run
    event_handler = ChangeHandler(directory, auto_clear, extensions)
    event_handler.run()

    # Setup watchdog
    observer = Observer()
    observer.schedule(event_handler, path=directory, recursive=True)
    observer.start()

    # Watch and run tests until interrupted by user
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
60330
def _run_cmd(cmd, ctx, glob, loc):  # pragma: no cover
    """"""
    if PDB:
        _enable_pdb()
    if IPYTHON:
        from IPython import start_ipython
        args_ipy = ['-i', '--gui=qt']
        ns = glob.copy()
        ns.update(loc)
        return start_ipython(args_ipy, user_ns=ns)
    # Profiling. The builtin `profile` is added in __init__.
    prof = __builtins__.get('profile', None)
    if prof:
        prof = __builtins__['profile']
        return _profile(prof, cmd, glob, loc)
    return exec_(cmd, glob, loc)
350276
def profile(ctx, filepath, calltree=False):
    

    filepath = pathlib.Path(filepath)
    if not filepath.is_file():
        log("profile", f"no such script {filepath!s}", LogLevel.ERROR)
    else:
        if calltree:
            log("profile", f"profiling script {filepath!s} calltree")
            ctx.run(
                (
                    f"python -m cProfile -o .profile.cprof {filepath!s}"
                    " && pyprof2calltree -k -i .profile.cprof"
                    " && rm -rf .profile.cprof"
                )
            )
        else:
            log("profile", f"profiling script {filepath!s}")
            ctx.run(f"vprof -c cmhp {filepath!s}")
120412
def run(self):
        """"""
        script = path.realpath(__file__)
        self.log.debug('Running from %s with arguments: %s', script, self.args)

        if self.args.source:
            self.source = self.args.source
        else:
            # hardcoding as the parent-parent of the script for now
            self.source = path.dirname(path.dirname(script))
        self.log.debug('Sourcing dotfiles from %s', self.source)

        try:
            if self.args.repo:
                self.clone_repo()

            self.deploy_dotfiles(self.load_dotfiles())

        except:
            self.log.exception('Profile deploy failed')

        finally:
            if self.args.repo:
                self.cleanup_repo()