title: Statistical Profiling in Python
body: <p>I would like to know that the Python interpreter is doing in my production environments.</p>
  
  <p>Some time ago I wrote a simple tool called <a href="https://github.com/guettli/live-trace" rel="noreferrer">live-trace</a> which runs a daemon thread which collects stacktraces every N milliseconds. </p>
  
  <p>But signal handling in the interpreter itself has one disadvantage:</p>
  
  <blockquote>
    <p>Although Python signal handlers are called asynchronously as far as the Python user is concerned, they can only occur between the “atomic” instructions of the Python interpreter. This means that signals arriving during long calculations implemented purely in C (such as regular expression matches on large bodies of text) may be delayed for an arbitrary amount of time.</p>
  </blockquote>
  
  <p>Source: <a href="https://docs.python.org/2/library/signal.html" rel="noreferrer">https://docs.python.org/2/library/signal.html</a></p>
  
  <p>How could I work around above constraint and get a stacktrace, even if the interpreter is in some C code for several seconds?</p>
  
  <p>Related: <a href="https://github.com/23andMe/djdt-flamegraph/issues/5" rel="noreferrer">https://github.com/23andMe/djdt-flamegraph/issues/5</a></p>