title: Profiling a long-running Python Server
body: <p>I have a long-running <a href="http://twistedmatrix.com/trac/" rel="nofollow noreferrer">twisted</a> server.</p>
  
  <p>In a large system test, at one particular point several minutes into the test, when some clients enter a particular state and a particular outside event happens, <em>then</em> this server takes several minutes of 100% CPU and does its work very slowly.  I'd like to know what it is doing.</p>
  
  <p>How do you get a profile for a particular span of time in a long-running server?</p>
  
  <p>I could easily send the server start and stop messages via HTTP if there was a way to enable or inject the profiler at runtime?</p>
  
  <p>Given the choice, I'd like <a href="https://stackoverflow.com/a/378024/15721">stack-based</a>/call-graph profiling but even leaf sampling might give insight.</p>