title: Proper use of QThread.currentThreadId()
body: <p>I thought that determining ID for the <code>QThread</code> currently running a function was <code>QThread.currentThreadId()</code>. However I find this does not give the expected results (in PyQt5 with python 3; but I have no reason to believe it would be different with pyqt4 / py 2 hence the generic tags). The thread Id varies in ways that I can't explain, indicating that I can't actually use it, where the QThread instance id varies predictably, indicating that I should be using that to identify currently running thread. To test, I created this: </p>
  
  <pre><code>from PyQt5 import QtCore, QtWidgets
  from PyQt5.QtCore import pyqtSignal
  import time
  import sys
  
  def logthread(caller):
      print('%-25s: %s, %s' % (caller, QtCore.QThread.currentThread(), QtCore.QThread.currentThreadId()))
  
  
  class Worker(QtCore.QObject):
      done = pyqtSignal()
  
      def __init__(self, parent=None):
          logthread('worker.__init__')
          super().__init__(parent)
  
      def run(self, m=10):
          logthread('worker.run')
          for x in range(m):
              y = x + 2
              time.sleep(0.001) 
          logthread('worker.run finished')
  
          self.done.emit()
  
  
  class MainWindow(QtWidgets.QWidget):
      def __init__(self, parent=None):
          logthread('mainwin.__init__')
          super().__init__(parent)
  
          self.worker = Worker()
          self.workerThread = None
  
          self.btn = QtWidgets.QPushButton('Start worker in thread')
          self.btn2 = QtWidgets.QPushButton('Run worker here')
          layout = QtWidgets.QVBoxLayout(self)
          layout.addWidget(self.btn)
          layout.addWidget(self.btn2)
  
          self.run()
  
      def run(self):
          logthread('mainwin.run')
  
          self.workerThread = QtCore.QThread()
          self.worker.moveToThread(self.workerThread)
          self.worker.done.connect(self.workerDone)
          self.btn.clicked.connect(self.worker.run)
          self.btn2.clicked.connect(self.runWorkerHere)
  
          self.workerThread.start()
          self.show()
  
      def workerDone(self):
          logthread('mainwin.workerDone')
  
      def runWorkerHere(self):
          logthread('mainwin.runWorkerHere')
          self.worker.run()
  
  
  if __name__ == '__main__':
      app = QtWidgets.QApplication([])
      logthread('main')
  
      window = MainWindow()
      sys.exit(app.exec_())
  </code></pre>
  
  <p>When you run it, the first 4 lines printed occur before the event loop is entered and show that the <code>QThread.currentThread()</code> Python id is different in several locations, but the <code>QThread.currentThreadId()</code> is same: </p>
  
  <pre><code>main                     : &lt;PyQt5.QtCore.QThread object at 0x01ABDD00&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  mainwin.__init__         : &lt;PyQt5.QtCore.QThread object at 0x01ABDD50&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  worker.__init__          : &lt;PyQt5.QtCore.QThread object at 0x01ABDDA0&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  mainwin.run              : &lt;PyQt5.QtCore.QThread object at 0x01ABDE90&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  </code></pre>
  
  <p>I expected all QThread Python id to be same, but ok maybe several instance of QThread wrap the same C++ thread pointer. </p>
  
  <p>Now click the "Run worker here" button: this just calls the <code>worker.run</code> method directly from GUI thread, so the method should indicate it is running in that thread. This prints these four lines: </p>
  
  <pre><code>mainwin.runWorkerHere    : &lt;PyQt5.QtCore.QThread object at 0x01ABDEE0&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  worker.run               : &lt;PyQt5.QtCore.QThread object at 0x01ABDEE0&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  worker.run finished      : &lt;PyQt5.QtCore.QThread object at 0x01ABDEE0&gt;, &lt;sip.voidptr object at 0x01A4ACC8&gt;
  mainwin.workerDone       : &lt;PyQt5.QtCore.QThread object at 0x01ABDEE0&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  </code></pre>
  
  <p>Indeed this time the <code>QThread</code> instance ID is same on all lines, nice to see. But the thread id is different in 3rd line, the line that gets printed by the slot as a result of signal in <code>worker.run</code>, yet signal was generated in same thread! Also, it means that the same <code>QThread</code> object can have several underlying thread id. </p>
  
  <p>Now click the "Start Worker". This calls <code>worker.run</code> but in the worker's thread. The 3 lines printed are: </p>
  
  <pre><code>worker.run               : &lt;PyQt5.QtCore.QThread object at 0x01ABDE90&gt;, &lt;sip.voidptr object at 0x01A4ABC0&gt;
  worker.run finished      : &lt;PyQt5.QtCore.QThread object at 0x01ABDE90&gt;, &lt;sip.voidptr object at 0x01A4ACC8&gt;
  mainwin.workerDone       : &lt;PyQt5.QtCore.QThread object at 0x01ABDEE0&gt;, &lt;sip.voidptr object at 0x01A4ACC8&gt;
  </code></pre>
  
  <p>The <code>QThread</code> instance ID is different inside the <code>worker.run</code> (first two lines) than in slot, this makes sense. Again the thread id (<code>sip.voidptr</code>) vary in ways that don't make sense to me: I would have expected line 2 to show thread id 0x01A4ABC0, same as first line, rather than same as 3rd line (the slot). </p>
  
  <p>Interestingly, if you replace the thread id output in <code>logthread</code> format by <code>QtWidgets.QApplication.instance().thread()</code>, you find that <code>QThread</code> instance id is <em>always</em> the same as the application QThread instance id except when the worker.run` is run in the separate thread. Even before the application event loop is entered (in other words the application thread id becomes constant only after the event loop starts). </p>
  
  <p>If I'm testing this right, the above indicate that <code>QThread</code> instance ID has consistent and predictable value once the <code>QApplication</code> event loop has started, but that the thread id (<code>currentThreadId()</code>) does not. Hence that whenever I want to test thread location of a function running I should use <code>QThread.currentThread()</code> and possibly compare to <code>app.thread()</code>, but I should avoid <code>currentThreadId()</code>. Anyone see any issues with the way I tested this and the conclusion? If no issues, how does this make sense, given the docs for <code>currentThreadId()</code>? If I made a mistake, what did I do wrong? </p>