I am trying to set up a dual task which plays participants a constant stream of tones every 3 seconds and records responses to them via mouse clicks, while at the same time they are viewing stimuli on screen and responding to them via key presses. I currently have this set-up running from 2 separate machines which is extremely clunky and allows a lot of room for experimenter and technical error so I am trying to integrate the scripts into 1, but am having no luck at all in getting the 2 processes to run simultaneously.
The audio task needs to run for as long as it takes participants to complete a set number of trials of the visual task, and the length of time taken to complete the visual task is variable dependent on participant reaction time. The 2 tasks should also be entirely independent of each other, apart from starting and finishing at the same time.
Essentially what I need is:
While (trial number not met):
Play sound + record responses
While sound is playing AND for set no. trials:
Generate visual stims + record responses
I have tried various combinations of while and for loops to make this work, but as I can’t get the 2 loops to run simultaneously I haven’t been able to get any closer than getting the program to alternate between playing 1 tone and doing 1 trial of the visual task.
I have also tried to set it up using multiprocessing (just using basic code to practise with as the full experiment code is very long and convoluted) but am getting a pickling error when I try to incorporate sound. See code below. The same code works fine when both processes are just printing things.
I am too much of a novice to even begin to figure out how to fix the pickling issue on my own! Can anybody help me figure out if it’s possible to use multiprocessing with .Sound (and .Visual, as I will need both!) or if there is a clever way to use a combination of loops to mean it isn’t necessary?
Thank you!
Practise code:
from multiprocessing import Process
from psychopy import sound
one_tone = sound.Sound(value='B',secs=0.2, octave=4, loops=0)
def sounds(tone):
for i in range (5):
tone.play()
core.wait(0.5)
def counting(total):
for i in range (total):
print i
core.wait(0.3)
if __name__ == '__main__':
p1 = Process(target=sounds, args=(one_tone,))
p2 = Process(target=counting, args=(20,))
p1.start()
p2.start()
p1.join()
p2.join()
error output
File “C:\Users\jud1\Box Sync\Adult WSLS experiments\Experiment 2 - multiple dual task pilots\Audio\multiprocess test.py”, line 26, in
p1.start()
File “C:\Program Files\PsychoPy2\lib\multiprocessing\process.py”, line 130, in start
self._popen = Popen(self)
File “C:\Program Files\PsychoPy2\lib\multiprocessing\forking.py”, line 277, in init
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File “C:\Program Files\PsychoPy2\lib\multiprocessing\forking.py”, line 199, in dump
ForkingPickler(file, protocol).dump(obj)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 224, in dump
self.save(obj)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 331, in save
self.save_reduce(obj=obj, *rv)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 425, in save_reduce
save(state)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 286, in save
f(self, obj) # Call unbound method with explicit self
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 655, in save_dict
self._batch_setitems(obj.iteritems())
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 687, in _batch_setitems
save(v)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 286, in save
f(self, obj) # Call unbound method with explicit self
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 554, in save_tuple
save(element)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 331, in save
self.save_reduce(obj=obj, *rv)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 425, in save_reduce
save(state)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 286, in save
f(self, obj) # Call unbound method with explicit self
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 655, in save_dict
self._batch_setitems(obj.iteritems())
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 687, in _batch_setitems
save(v)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 331, in save
self.save_reduce(obj=obj, *rv)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 396, in save_reduce
save(cls)
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 286, in save
f(self, obj) # Call unbound method with explicit self
File “C:\Program Files\PsychoPy2\lib\pickle.py”, line 754, in save_global
(obj, module, name))
pickle.PicklingError: Can’t pickle <type ‘Sound’>: it’s not found as builtin.Sound