The three functions below show how I'm using the pyglet library to draw squares on the screen. The code works fine. But I want to make it run faster.
With my novice understanding of threading, I think the for-loop in draw_grid() can be made faster by using threads, as it waits for one square to draw using pos and then draws a new square. Since all_positions is already provided, is there a way to draw all squares simultaneously?
def draw_square(single_location):
# draws a single square
pyglet.graphics.draw_indexed(4, pyglet.graphics.gl.GL_TRIANGLES,
[0,1,2,1,2,3],
('v2i', single_location))
def position_array():
# returns an 2D array with each row representing the coordinates of the rectangle to draw
...relevant code...
return all_positions
def draw_grid(all_positions):
# uses draw_square function and all_positions to draw multiple squares at the specified locations.
for pos in all_positions:
draw_square(pos)
After looking up some videos on threading, I found the concurrent.futures library. So I tried to implement it and it gave me errors. So now I am stuck.
Here's how I used concurrent.futures.ThreadPoolExecutor() inside draw_grid()
def draw_grid(all_positions):
# uses draw_square function and all_positions to draw multiple squares at the specified locations.
with concurrent.futures.ThreadPoolExecutor as executor:
for pos in all_positions:
f1 = executor.submit(draw_square, pos)
f1.result()