Howdy, Stranger!

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

Supported by

[solved] pygaze multithreading

edited October 2014 in PyGaze

Hi,
Thanks for this set of libraries, it looks very much like what I need. I'm fairly new to python and this is helping me progress quickly.
Currently I want to present an animated fixation screen while waiting for a condition to come true. I tried launching the function for the animation (basically looping through a set of screens) in a separate thread with the idea of testing the condition in the main thread and killing the fixation thread when the condition became true, however this approach seems to cause a seg fault.
Is is possible to use pygaze in a multi-threaded environment? If so are there any examples of how this can be done?
Thanks
Tom

Comments

  • edited 11:52AM

    Hi Tom,

    Thanks, and welcome to the forum! Whether PyGaze is multithreading-safe depends on what OS you use, and what you're doing in each of the threads.

    Under Linux, trying to communicate with the X server from different threads results in a hard crash, which means that if you're displaying your animated fixation via the main thread, and simply check for a timeout in a subthread you should be ok. However, if you're parsing samples in the subthread, using the one of the eyetracker classes, you might run into trouble (because these classes have built-in display functionality). As you suggest doing the opposite (running the fixation in the subthread, while checking for the condition in the main thread), you're going to have a problem under Linux anyway. Surprisingly, this is no problem under Windows (where Python's multithreading module behaves differently).

    You could opt for a sequential solution, by doing both the checking for your condition and displaying the animated fixation cross from within the same loop. If you're not doing anything too complicated, this shouldn't affect the animation smoothness. An example:

    ###
    # imports, preparation, and other stuff here
    ###
    
    # central fixation position
    fixpos = (DISPSIZE[0]/2, DISPSIZE[1]/2)
    
    # iterator and sentry variable
    i = 0
    condition = False
    
    # loop until a condition is met
    while not condition:
        # check for a sample on fixpos
        gazepos = tracker.sample()
        if (gazepos[0] - fixpos[0])**2 + () < maximal_error:
            contition = True
        # update the animation
        disp.fill(animfixscreens[i])
        disp.show()
        # update iterator
        if i >= len(animfixscreens)-1:
            i = 0
        else:
            i += 1
        # optional: pause for a bit
        # (not necessary when using blocking flip under PsychoPy)
        # libtime.pause(10)
    

    This snippet shows how you loop through a list of Screen objects, animfixscreens (each containing a different frame of your animated fixation), whilst also checking whether the current gaze position is at the desired fixation position. You can add complexity as you like, for example to keep track of how many consecutive samples have been at the desired fixation location and to break after a certain number (or fixation duration).

    Does this help you at all? If not, please elaborate on the specifics of your experiment are (i.e.: what condition are you checking for, and how is your animated stimulus presented).

    Good luck!

    Edwin

Sign In or Register to comment.