title: Behavior of Python's time.sleep(0) under linux - Does it cause a context switch?
body: <p>This pattern comes up a lot but I can't find a straight answer.</p>
  
  <p>An non-critical, un-friendly program might do</p>
  
  <pre><code>while(True):
      # do some work
  </code></pre>
  
  <p>Using other technologies and platforms, if you want to allow this program to run hot (use as much CPU cycles as possible) but be polite - allow other programs who are running hot to effectively slow me down, you'd frequently write:</p>
  
  <pre><code>while(True):
      #do some work
      time.sleep(0)
  </code></pre>
  
  <p>I've read conflicting information about whether the latter approach would do what I'd hope on python, running on a linux box.  Does it cause a context switch, resulting in the behavior I mentioned above?</p>
  
  <p>EDIT: For what's worth, we tried a little experiment in Apple OSX (didn't have a linux box handy).  This box has 4 cores plus hyperthreading so we spun up 8 programs with just a</p>
  
  <pre><code>while(True):
      i += 1
  </code></pre>
  
  <p>As expected, the Activity Monitor shows each of the 8 processes as consuming over 95% CPU (apparently with 4 cores and hyperthreading you get 800% total).  We then spun up a ninth such program.  Now all 9 run around 85%.  Now kill the ninth guy and spin up a program with</p>
  
  <pre><code>while(True):
      i += 1
      time.sleep(0)
  </code></pre>
  
  <p>I was hoping that this process would use close to 0% and the other 8 would run 95%.  But instead, all nine run around 85%.  So on Apple OSX, sleep(0) appears to have no effect.</p>