【发布时间】:2020-05-11 12:59:04
【问题描述】:
下面的三个函数展示了我如何使用 pyglet 库在屏幕上绘制正方形。代码工作正常。但我想让它运行得更快。
以我对线程的新手理解,我认为draw_grid() 中的 for 循环可以通过使用线程来更快,因为它等待使用pos 绘制一个正方形,然后绘制一个新正方形。既然已经提供了all_positions,有没有办法同时绘制所有的正方形?
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)
在查找了一些关于线程的视频后,我找到了concurrent.futures 库。所以我试图实现它,它给了我错误。所以现在我被卡住了。
这是我在 draw_grid() 中使用 concurrent.futures.ThreadPoolExecutor() 的方法
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()
【问题讨论】:
标签: python multithreading pyglet concurrent.futures