While loop until routine ends


I’m trying to implement a while loop in my experiment. The while loop should end when a stimulus component ends.

Let’s suppose I present a stimulus for 4 seconds (component name stimulus), during these 4 seconds I want a loop to run, but after the 4 seconds the loop should end.

What I did so far is, in Each Frame:

if stimulus.status == STARTED:
    while stimulus.status != FINISHED:
        print("stimulus still not finished") #just an example

However, the experiment gets stuck and crashes. Is there a solution to this problem?

Additional infos
Further read, in case knowing what I wanna do could actually help.

I’m setting up an fMRI experiment and I would like to store whenever I pulse from the fMRI machine is received.

I tried using the method above, storing until the stimulus was not over, or just putting a simple plain code, always in Each Frame

pins = list(range(2,14))
pins.append(15) #pins to check 

for pin in pins:
    signal = p_port.readPin(pin) #get the information from the scanner

    if (pin in range(1,10) and signal == 1 or pin in range(10,16) and signal == False):
    print("pin received")

However, also this code doesn’t actually tell me when the pin is received.