title: Starting two methods at the same time in Python
body: <p>I am trying to run two methods at the same time in Python. One of them plays a sound and the other one records it. Both methods work fine but I could not figure it out how to start them at the same time both multiprocessing and threading were tried. Whereas I am almost sure now that it can't be solved with threading.</p>
  
  <pre><code>def listen_to_audio()
  
  def play_audio()
  </code></pre>
  
  <p>Any ideas? (They don't have to finish at the same time but they should both start within a second.)</p>
  
  <p>That is the code, sorry for not posting it in the beginning:</p>
  
  <pre><code>import pyaudio
  import wave
  import sys
  import time
  from math import *
  from getopt import *
  import threading
  
  def make_sin(f0=1000.,ampl=30000,rate=22050,length=5.):
    a = 2. * pi * f0/rate              
    n = int(rate * length)
    wav=''
    for i in range(0, n):
      f = int(ampl*sin(a*i))
      wav += chr(f &amp; 0xFF) + chr((f &amp; 0xFF00) &gt;&gt; 8)
    return wav
  
  
  
  def play_audio(forHowLong):
      data = make_sin(f0=1000.,ampl=30000,rate=22050,length=5.)
  
      p = pyaudio.PyAudio() #sets up portaudio system
      stream = p.open(format=p.get_format_from_width(2),
                  channels=1,
                  rate=22050,
                  output=True)
  
      start = time.time()
      while time.time() &lt; start + forHowLong*0.5:    
              stream.write(data)
  
      stream.stop_stream()
      stream.close()
      p.terminate()
  
  
  def listen_to_audio(forHowLong):
      CHUNK = 1024
      FORMAT = pyaudio.paInt16
      CHANNELS = 2
      RATE = 44100
      RECORD_SECONDS = forHowLong
      WAVE_OUTPUT_FILENAME = "output.wav"
  
      p = pyaudio.PyAudio()
  
      stream = p.open(format=FORMAT,
              channels=CHANNELS,
              rate=RATE,
              input=True,
              frames_per_buffer=CHUNK)
  
      print("* recording")
  
      frames = []
  
      for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
          data = stream.read(CHUNK)
          frames.append(data)
  
      print("* done recording")
  
      stream.stop_stream()
      stream.close()
      p.terminate()
  
      wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
      wf.setnchannels(CHANNELS)
      wf.setsampwidth(p.get_sample_size(FORMAT))
      wf.setframerate(RATE)
      wf.writeframes(b''.join(frames))
      wf.close()
  
  
  def main():
  
  #start play_audio and record_audio here at the same time
  
  
  
  if __name__ == "__main__":
      main()
  </code></pre>