Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Simple coroutine question

edited June 2016 in OpenSesame


The coroutine function is fantastic, but I'm not able to get the coroutine to end after a button press. Basically I present a stimulus for 500ms, it is replaced by a blank screen for up to 1500ms (before a 2000ms timeout - and this is where the coroutine function came in handy). But I've tried setting the duration of the coroutine to 'keypress' and it doesn't seem to work - am I missing something very obvious?

Thank you!


  • edited 9:48PM


    That's an interesting question. Right now, coroutines has a fixed duration. But what you could do is define your own generator function in an inline_script that

    1. Checks whether a response has been given; and if so
    2. Changes the coroutines duration to 0, triggering an immediate timeout.

    Like so:

    def break_coroutines():
        """A generator that breaks a coroutine when a response is given."""
        # To start, set response to None
        var.response = None
        # Loop while coroutines is running
        while True:
            # If a response is given, set the coroutines duration to 0, causing
            # it to stop
            if var.response is not None:
                items['my_coroutines'].var.duration = 0
            # Also break if the coroutines signals that it's over
            keep_going = yield
            if not keep_going:

    You'd then add 'break_coroutines' in the 'Generator function name' field of the coroutines plug-in.

    It's a bit of a hack though ... I'll see if I can think of a more elegant way to do this.


  • edited 9:48PM

    Hi Sebastiaan,

    Great thank you! I will give it a shot and let you know how it goes.


  • edited 9:48PM

    Hi Sebastiaan,

    So I am getting a 'zero division error' - float division by zero. It seems like the loop in the inline script isn't working: if I set items['my_coroutines'].var.duration = 1 then I see subsequent trials for 1ms, so the script is not only applying the 'set coroutines duration to 0' to a single trial, but to every subsequent trial.

  • edited 9:48PM

    Ow right! You need to set duration back to its original value before the coroutines is started. To do so, you can add an inline_script immediately before the coroutines plug-in, with the following code in it (either in the Run or Prepare phase):

    # Replace 5000 by the original duration of the coroutines
    items['my_coroutines'].var.duration = 5000
  • edited 9:48PM

    Works great! I placed it in the prepare phase of the main script you passed along - a super useful fix!

Sign In or Register to comment.