def live_profile(script, argv, profiler_factory, interval, spawn, signum,
                 pickle_protocol, mono):
    """Profile a Python script continuously."""
    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)
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 : os . close ( stderr_w_fd ) viewer , loop = make_viewer ( mono ) 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 : os . kill ( pid , signal . SIGTERM ) raise finally : parent_sock . close ( ) w_pid , status = os . waitpid ( pid , os . WNOHANG ) if w_pid == 0 : os . kill ( pid , signal . SIGTERM ) exit_code = os . WEXITSTATUS ( status ) with os . fdopen ( stderr_r_fd , 'r' ) as f : child_stderr = f . read ( ) if child_stderr : sys . stdout . flush ( ) sys . stderr . write ( child_stderr ) sys . exit ( exit_code ) else : os . close ( stderr_r_fd ) devnull = os . open ( os . devnull , os . O_RDWR ) for f in [ sys . stdin , sys . stdout ] : os . dup2 ( devnull , f . fileno ( ) ) 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 )
Profile a Python script continuously.