title: How can I break out of a real-time while loop gathering data without using the keyboard or keyboard interrupt?
body: <p>So I'm continuously measuring temperature data with a National Instruments data acquisition system, and I've been able to plot the real time temperature measurements and store that into a dataframe that I can then save.</p>
  <p>What I'm trying to do is set the &quot;samplerate&quot; range to be something really high (its at 301 right now) and then stop it when I no longer want to acquire temperature data. Sometimes I need to collect only 100 samples, sometimes over 10k.</p>
  <p>When I use this code to break out the loop it prints into the console what I ask, but I cant actually type anything into the console. The console is completely unresponsive to keyboard interrupts. <a href="https://i.stack.imgur.com/nPyi1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nPyi1.png" alt="enter image description here" /></a></p>
  <p>Is there anyway that I can break out of this loop early without out using the keyboard?</p>
  <pre><code>import nidaqmx
  import matplotlib.pyplot as plt
  import pandas as pd
  
  plt.ion()
  
  Samples = []
  time = []
  freezerTemp = []
  rightSideTemp = []
  
  def print_menu():
      print('Press &quot;3&quot; and hit &quot;Enter&quot; to exit ')
      print('3. Exit')
  
  with nidaqmx.Task() as task:
  
      FreezerT = task.ai_channels.add_ai_thrmcpl_chan(&quot;cDAQ1Mod1/ai0&quot;)
      RightSideT = task.ai_channels.add_ai_thrmcpl_chan(&quot;cDAQ1Mod1/ai3&quot;)
      #This controls the sample/second. i.e. if 2 then 2 samples/second.
      samplerate = task.timing.samp_clk_rate = 1
  
      while True:
          print_menu()
  
          for samplerate in range(301):
              data = task.read()
              Samples.append(samplerate)
              freezerTemp.append(data[0])
              rightSideTemp.append(data[1])
              time.append(samplerate/1)
  
              plt.xlabel('Samples')
              plt.ylabel('Temperature (C)')
              plt.scatter(samplerate,freezerTemp[samplerate],c='r')
              plt.scatter(samplerate,rightSideTemp[samplerate],c='b')
              plt.pause(0.05)
              plt.show()
  
          choice = input('Enter a number: ')
          if choice == '3':
              break
  
  df = pd.DataFrame({'Samples': Samples,
                      'Time(sec)': time,
                  'Freezer Temperature': freezerTemp,
                  'Right Side': rightSideTemp})
  
  print(df)
  
  </code></pre>