import sys, py, os def wrapper(func_to_run): def f(frame, event, arg): func_to_run() return f return f def run_with_tracing(f, func_to_run, args=()): sys.settrace(wrapper(func_to_run)) f(*args) sys.settrace(None) def trace_in_another_process(filename, source_code, func_name, measure_func, executable, args=()): source = py.code.Source(source_code, """ import sys sys.path.insert(0, '%s') from tracer import run_with_tracing def write(): sys.stdout.write('c') sys.stdout.flush() sys.stdin.read(1) if __name__ == '__main__': run_with_tracing(%s, write, %s) sys.stdout.write('F') sys.stdout.flush() """ % (py.magic.autopath().dirpath(), func_name, args)) f = py.path.local(filename) f.write(source) cmd = "%s -u %s" % (executable, f) stdout, stdin = os.popen2(cmd) while 1: ch = stdin.read(1) if ch == 'F': break assert ch == 'c' stdout.write('c') stdout.flush() measure_func()