I don't have enough knowledge about Pyaudio and I couldn't find it on the net. I want the script run until I stop it and when I stop, it must save recording. The sample code from Pyaudio records for a fixed time.
What you need to do is use error handling to save the audio once you have a keyboard exception, (in this case pressing something like Ctrl + C
to end the program.
While True:
try:
recording_function()
except KeyboardInterrupt:
save_function() #record to a file
except:
#generic error processing