python freezing/not responding in 300 trial loop
Hi, we're trying to run a loop of 300 trials of a shape stimulus with an optional key response. After some amount of trials (not always the same), python is going non-responsive and the task is freezing in place. In some cases, we can esc and quit, in some cases the whole computer is frozen.
The images are presented by creating two lists, on with the shape name, one with an inter-stimulus interval time, both of which are randomized. In the loop:
#Prepare tab: from openexp.canvas import canvas shape = shapes.pop() #from the randomized list ISI = timings.pop() self.experiment.set("trial_shape", shape) self.experiment.set("trial_ISI", ISI) canvas_shape = canvas(exp) canvas_blank = canvas(exp) #Run tab: #my_keyboard = Keyboard(keylist=[1], timeout=0) #my_keyboard.flush() duration_shape = 100 duration_blank = self.experiment.get('trial_ISI') if shape == 'yellow_square': canvas_shape.rect(-43, -43, 86, 86, fill=True, color='yellow') elif shape == "blue_square": canvas_shape.rect(-43, -43, 86, 86, fill=True, color='blue') elif shape == "green_square": canvas_shape.rect(-43, -43, 86, 86, fill=True, color='green') elif shape == "red_square": canvas_shape.rect(-43, -43, 86, 86, fill=True, color='red') elif shape == 'yellow_circle': canvas_shape.circle(0, 0, 43, fill=True, color='yellow') elif shape == 'blue_circle': canvas_shape.circle(0, 0, 43, fill=True, color='blue') elif shape == 'green_circle': canvas_shape.circle(0, 0, 43, fill=True, color='green') elif shape == 'red_circle': canvas_shape.circle(0, 0, 43, fill=True, color='red') elif shape == 'yellow_triangle': canvas_shape.polygon([(0,-43), (50,43), (-50,43)], fill=True, color='yellow') elif shape == 'blue_triangle': canvas_shape.polygon([(0,-43), (50,43), (-50,43)], fill=True, color='blue') elif shape == 'green_triangle': canvas_shape.polygon([(0,-43), (50,43), (-50,43)], fill=True, color='green') elif shape == 'red_triangle': canvas_shape.polygon([(0,-43), (50,43), (-50,43)], fill=True, color='red') elif shape in ['yellow_star', 'blue_star', 'green_star', 'red_star']: n1 = -5, -70 n2 = 5, -20 n3 = 55, 0 n4 = 15, 20 n5 = 25, 70 n6 = -5, 40 n7 = -35, 70 n8 = -25, 20 n9 = -65, 0 n10 = -15, -20 if shape == 'yellow_star': starColor = 'yellow' elif shape == 'blue_star': starColor = 'blue' elif shape == 'green_star': starColor = 'green' elif shape == 'red_star': starColor = 'red' canvas_shape.polygon([n1, n2, n3, n4, n5, n6, n7, n8, n9, n10], fill=True, color=starColor) # Element interface canvas_shape['star'] = Polygon([n1, n2, n3, n4, n5, n6, n7, n8, n9, n10], fill=True, color=starColor) show_time_shape = canvas_shape.show() exp.set('onset_time_shape', show_time_shape) while self.time() - show_time_shape < duration_shape: # if not key: # key, time = my_keyboard.get_key() print() # Show the second canvas, and poll the keyboard for the # specified duration. show_time_blank = canvas_blank.show() while self.time() - show_time_blank < duration_blank: # if not key: # key, time = my_keyboard.get_key() print() exp.set('response', key) if key: exp.set('response_time', int(round(time-show_time_shape))) else: exp.set('response_time', 0)
I've commented out the key response functionality in trying to diagnose what is causing the issue. Also have tried this using
path = exp.get_file("stimuli/%s.png"%exp.get("trial_shape"))
canvas_shape.image(path)
instead of shape functions, still having the same issue. There's no error being thrown, have tried enabling garbage collection and disabling.
Comments
a note: changing the while loops to clock.sleep(duration) for each canvas solves the issue, so the while loop seems to be the problem, however it doesn't allow for accurate recording of response time by continuous checking of get_key(), and I've seen that countdown time logic for the while loop suggested in other conversations.
Note two for future devs: code for timing example is here. I found adding get_key(timeout=0) helped with the freezing problem.
Note two: problem is CPU usage during the while loops.
Hi @kazald,
I replied in your other discussion on this issue. Please don't double post your question, as this makes it harder to keep the overview.
Eduard