0

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

  • 1
    Hi! Do you have your current code? It is hard to guess what is happening without it – smagnan Feb 07 '22 at 21:38
  • @smagnan sadly no, I deleted it when trying to solve the problem. Thought I get a fresh start. Although this pretty much explains everything of the procedure. I started the thread like this : threading.Thread(function , args=[1]) –  Feb 07 '22 at 21:41
  • did you start your thread? For example, `t = threading.Thread(function , args=[1])` *creates* your thread object, but you need to `t.start()` for the thread to actually start running @greyCat – smagnan Feb 07 '22 at 21:46
  • Was `is_done` declared to be global everywhere that it was used. It's possible one of the `is_done`s is a local variable. Again, this would be a lot easier with code. – Frank Yellin Feb 07 '22 at 21:49
  • @smagnan I started it. I printed a line in the "thread" and a line outside the "thread" and it shows that they ran in the same thread, the MainThread. –  Feb 07 '22 at 21:50
  • @FrankYellin Yes, I remember I declared it as global everywhere I used it. I don't think it's a problem with that variable since it just ran in the same thread when I started it. I am sorry about the inconvenience, I will try to recreate the code. –  Feb 07 '22 at 21:53
  • @smagnan I added the code to question , what do you think the problem is? keep in mind python is a new language to me so I don't know much about it –  Feb 07 '22 at 22:15
  • @grayCat I was going to explain the issue but seems your answer got closed for duplicate (see link at the top). In short you are calling you `capture_frames()` when you create the thread, so it runs the function like a normal one and forever hands on the `while`. You should be doing this instead: `threading.Thread(target=capture_frames, args=[1]).start()` (no parenthesis) – smagnan Feb 07 '22 at 22:24

0 Answers0