How To: Simultaneous Tone Task and Stimulus Presentation?

Hi! I have an issue that I cannot find a way around. I have an experiment for which I show a grid with white circles. At the samw time I want a tone discrimination task to happen, so the tones to be plaxed and the mouse clicks to be recorded.
For the grids, I iterate through this loop four times and each time, a different circle gets colored blue. This code I added in the begin routine part

for i in range(4):  
    # Create the grid with all dots initially white
    colors_array_30 = np.full((num_dots, 3), [1, 1, 1])  # Initialize with white
    colorgrid_30 = visual.ElementArrayStim(win, nElements=num_dots, sizes=2*radius,
                                        elementTex=None, elementMask='circle', xys=dots_30, colors=colors_array_30.tolist())

    # Randomly select a dot to color blue
    selected_dot = np.random.choice(dot_list_30)
    # Check if the selected dot has already been colored
    while selected_dot['label'] in colored_dot_labels:
        selected_dot = np.random.choice(dot_list_30)

    selected_dot['colored'] = True  # Mark the selected dot as colored

    # Append the label of the colored dot to the list
    colored_dot_labels.append(selected_dot['label'])
    

    # Change the color of the selected dot to blue
    for dot in dot_list_30:
        if dot['colored']:
            colors_array_30[dot_list_30.index(dot)] = [0, 0, 1]  # Set the color of the dot to blue

    # Draw the grid with the colored dot
    frame.draw()
    border_grid_30.draw()
    colorgrid_30.colors = colors_array_30.tolist()  # Update the colors
    colorgrid_30.draw()
    win.flip()
    core.wait(0.745)  # Wait 
    # Erase the colored dot
    colors_array_30 = np.full((num_dots, 3), [1, 1, 1])  # Reset to all white
    colorgrid_30.colors = colors_array_30.tolist()  # Update the colors
    colorgrid_30.draw()
    win.flip()
    core.wait(0.245)  # Wait for 0.5 seconds before drawing the next dot

    # Reset dot_list to mark all dots as not colored
    for dot in dot_list_30:
        dot['colored'] = False

In a following routine, I implemented code for a tone discrimination task. Here, a tone is played, followed by silence. For each tone and silence, a mouse click is recorded. This code is in the Each Frame component.

# Initialize mouse clicks variable outside the loop
mouse_clicks = []
clock.reset()
# Play the first silence
core.wait(silence_to_play[0])
played_silences.append(silence_to_play[0])  # Remove and append the first silence
silence_to_play = silence_to_play[1:]

# Loop for playing tones and silences
for tone_name, silence_file in zip(tone_names_to_play, silence_to_play):
    tone_sound = tones[tone_name]

    mouse.clickReset()
   
    # Play tone
    tone_sound.play()
    played_sounds.append(tone_name)
    # Wait for the tone to finish playing
    core.wait(tone_sound.getDuration())

    # Introduce silence period for mouse clicks
    core.wait(silence_file)
    played_silences.append(silence_file)
    
    buttons, times = mouse.getPressed(getTime=True)
    # Record mouse events
    mouse_clicks.append({'buttons': buttons, 'times': times})
        # Determine correct_side variable
    if tone_name == 'tone_low':
        if times[0] != 0:
            correct_side = 1
        else:
            correct_side = 0
    elif tone_name == 'tone_high':
        if times[-1] != 0:
            correct_side = 1
        else:
            correct_side = 0
    else:
        correct_side = None  # Handle other cases if needed
    
    correct_sides.append(correct_side)  # Append correct_side to the list
    # Determine correct_timing variable
    if any(t > 0.65 for t in times):
        correct_timing = "timeout"
    elif all(t == 0 for t in times):
        correct_timing = None
    else:
        correct_timing = "goodtiming"
    
    correct_timings.append(correct_timing)  # Append correct_timing to the list
    
    # Determine score
    if correct_timing == "goodtiming" and correct_side == 1:
        score = 1
    else:
        score = 0

Now I want these two things to happen at the same time, in one single routine.The circles should be colored as the tones are played and mouse clicks are recorded. As the timings of the tones and silences are not the same as for the circles, these need to run at the same time, but independent of each other. I tried to work with threading, but so far this has not worked.

Any help would be appreciated!!