using this tutorial I was able to run a thread using threading.Thread(). In the thread I want to run code that runs in a loop until a Boolean is changed in the MainThread. I started that thread and the code in my function started to run, it looked something like this:
while is_done:
# do something
when I ran the thread it didn't run concurrently to the MainThread, I checked the current thread in each "thread" and the results are as follows:
before running:MainThread
inside the thread:MainThread
why does it not create a new Thread to run my code on it?
Another question, Will there be a visibility problem here? I know that in java there will be, The thread will run forever even after we change the Boolean in the mainthread but python maybe does it another way.
Thank you
Sadly, I deleted the code. Here is a recreation of it:
# audio
frames = []
sample_size = 0
audio_recording = False
#capture frames and store them in the frames[] array
def capture_frames():
global audio_recording, sample_size
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
while audio_recording:
frames.append(stream.read(CHUNK))
stream.stop_stream()
stream.close()
p.terminate()
#start the thread
def start_audio_recording():
global audio_recording, sample_size
# start thread for capturing audio
audio_recording = True
threading.Thread(target=capture_frames(), args=[1]).start()
#stop and write the recorded data to a file
def write_audio_recording():
global audio_recording, sample_size
audio_recording = False
wf = wave.open("audio_capture.wav", 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(2)
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
frames.clear()
so, if I call def start_audio_recording() it will block the code on the MainThread even though it is supposed to run on another thread