title: Question about python threads timing and how they compare to a ROS implementation
body: <p>I don't have a particular problem that I have to solve, but a question about (python) threads and their speed of execution.</p>
  <p><strong>The background</strong>
  (or situation 1)</p>
  <p>I have a python script using ROS that basically is (simplifying it):</p>
  <pre><code>class Viewer:
      def __init__(self):
          rospy.init_node(&quot;viewer&quot;)
  
          rospy.Subscriber(&quot;/message&quot;,
                           MessageType, self.onMessage)
  
          _, self.ax = plt.subplots(1, 1)
          self.ax.set_ylim((-self.ax_lim, self.ax_lim))
          self.ax.set_xlim((-self.ax_lim, self.ax_lim))
          self.ax.set_aspect('equal')
          self.ax.grid()
          self.textvar = self.ax.text(-self.ax_lim-10, -self.ax_lim-10, '0.0')
          plt.show()
      def onMessage(self,msg):
          #Here do some plotting
          
          plt.pause(0.01) #&lt;------NOTICE THIS!
          #Here remove the plotting
  </code></pre>
  <p>As you see there is a ROS node that shows a matplotlib canvas and everytime we receive a message some plotting is done there. Notice there is a small pause</p>
  <p><strong>The threaded code</strong> (situation 2)</p>
  <p>I have tried to implement something similar without ROS
  So I have</p>
  <pre><code>class Viewer:
      def __init__(self):
          _, self.ax = plt.subplots(1, 1)
          self.ax.set_ylim((-self.ax_lim, self.ax_lim))
          self.ax.set_xlim((-self.ax_lim, self.ax_lim))
          self.ax.set_aspect('equal')
          self.ax.grid()
          self.textvar = self.ax.text(-self.ax_lim-10, -self.ax_lim-10, '0.0')
          
          self.i=0
  
          #The threaded part
          x = threading.Thread(target=self.thread_function, args=(1,))
          x.start()
          plt.show()
  
      def thread_function(self,name):
          while(self.i&lt;5000):
              #Do some plotting here
              self.i+=1
              plt.pause(0.01)  #&lt;-----NOTICE THIS
              #here remove the plotting
  </code></pre>
  <p>The number of messages received by the situation 1 is around 5000 (therefore in situation 2 I put 5000 in the while loop</p>
  <p>Now the thing is, written as it is above the situation 2 program takes more than 40 minutes!! to complete, while the situation 1 program completes in a couple of minutes</p>
  <p>I tried commenting the <code>plt.pause</code> in the second one and it works much better. It completes in 4 minutes</p>
  <p>My question is, why is the threaded version much slower than the first one. Take into account that the plot operations are the same.</p>