Trouble with executing original function in Psychopy

Hi Michael,

Thank you for your detailed response - I’m new to Psychopy/Python so it’s really helpful to hear how the Builder functions in terms of running code, and how this relates in general to Python.

Following your advice, I’ve used threading in order to execute my function in line with running Psychopy. For anyone who is faced with the same problem in the future I’ve pasted the solution below.

In the code component of my ‘HeatStim’ routine (see above) in the ‘Begin Experiment’ tab I import the relevant libraries/modules for my function and for threading:

#import the library needed for using the thermodes 

import nidaqmx
import nidaqmx_analog_out

#import threading module and allocate one function to the pool

from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=1)

Now when I call my function in the ‘Each Frame’ tab I use the syntax:

#calling thermode stimulation function

if frameN == 1:
    Thermode_start = pool.apply_async(nidaqmx_analog_out.analog_in_out, (temp_val, DeviceChannel))

At the end of my last routine of my trial in the ‘End Routine’ tab of a newly inserted code component, I then save an output from my function which was executed in the thread with the syntax:

input_val= Thermode_start.get()
RepeatTrial.addData('analog_in', input_val)

I found the following documentation useful for figuring out how to do this: https://docs.python.org/2/library/threading.html#thread-objects and https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python

Thank you again Michael for your help - without your guidance I wouldn’t have figured out the need to use threading!