title: Python v3 threading and OS context-switching (changed from v2?)
body: <p>I hit the dead end with python 3 threading. Example script is somehow trivial while its behaviour is different compared to previous python version (tested with python 2.7).<br>
  The idea is to start function (<code>to_time_it()</code>) which waits a thread (<code>thread_it()</code>) to clear some flag. Thread is running continuosly until the very end, and sleep() inside both functions ensure that python must pool flags with <code>while</code> statement.</p>
  
  <p>The problem is, python v2(.7 in my case) runs faster, almost deterministicly faster, about (10 + 5) = 15 ms on my HP (win10, Intel i5, 32bit python v2.7) or some older linux pc. Although the same code run OK on python 3.6, something is causing this 10ms delay, <strong>except</strong> if there is some pointless <code>print</code> or <code>sleep(0)</code> inside certain while loop. Here is the code:</p>
  
  <pre class="lang-py prettyprint-override"><code># -*- coding: utf-8 -*-
  
  import os
  import time as systime
  import timeit
  import threading
  
  flag = False
  thread_flag = False
  
  counter = 0
  
  AVERAGE = 10
  
  def thread_it():
      global flag
      global thread_flag
  
      #run thread until thread_flag is not set
      while not thread_flag:
          while flag:
              # (1)
              #print("")
              #systime.sleep(0)
              pass
  
          #print("!")
          systime.sleep(0.05) # force to_time_it() to wait thread to set flag
  
          flag = True # return to thread while loop
  
  def to_time_it():
      global flag
      global counter 
  
      flag = False    # break out of thread while loop 
      #   - &gt; to systime.sleep() after #(1) and than set flag
  
      # wait thread to set flag (thread is than waiting for new cycle in it's while loop)
      systime.sleep(0.03) 
  
      while not flag: # wait for flag (approx 20ms)
          # (2)
          #print("")
          #systime.sleep(0)
          pass    # do nothing
  
      print("OK: %s" %counter) # thread has cleared flag, can start new cycle
      counter += 1
  
  
  def main():
      global thread_flag
      global flag
  
      print("Threading while loop test")
  
      th = threading.Thread(target=thread_it)
      th.start()
  
      tajm = timeit.timeit(to_time_it, number=AVERAGE) * 1000 / AVERAGE
  
      systime.sleep(0.7)  #not neccesary, just to see what is happening when printing out data
  
      # exit/end thread
      thread_flag = True
      flag = False
  
      print("\n\tAverage time: %.2f ms" % tajm)
  
      #os._exit(1)
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>The point where this code can run equally fast on both python versions is marked with <code># (1)</code> and <code># (2)</code>. It turns out, that a simple <code>print("")</code> or <code>time.sleep(0)</code> make some internal change that causes to disapear this delay when running on python 3. Adding any of it to point <code>#(1)</code> causes 10ms delay to disapear, and adding anything to point <code>#(2)</code> causes the other 5ms to disapear. I don't know why <code>while something: pass</code> is treated differently than <code>while something: time.sleep(0)</code>. Moving <code>pass</code>, <code>sleep()</code> or <code>print()</code> to external function has no effect.  </p>
  
  <p>I assume some changes about python threading module must have occured in v3, causing OS context switch / process or thread priority handling to act differently on newer version. Can anyone test this and confim/explain?</p>
  
  <p><strong><em>Note:</strong> <a href="https://stackoverflow.com/questions/48198172/python-v2-7-and-v3-6-behave-differently-but-the-same">I already started a similar question</a>, but I didn't get any responses. I started a new topic (not comment/edit) because I managed to strip down test script dependencies of pyserial and because I belive this is a more general python issue. Please, redirect me to better place or let me know what are other ways of getting experts to look at this case.</em></p>