Added parallel downloading of thumbnails.

By using asyncio.gather, all thumbnail-downloading tasks are queued
simultaneously, and downloaded in parallel by the task executor.
This commit is contained in:
Sybren A. Stüvel 2016-03-14 09:33:43 +01:00
parent b274faf12c
commit 8533ac83ba

View File

@ -179,12 +179,10 @@ async def fetch_texture_thumbs(parent_node_uuid: str, desired_size: str,
'projection': {'filename': 1, 'variations': 1, 'width': 1, 'height': 1}, 'projection': {'filename': 1, 'variations': 1, 'width': 1, 'height': 1},
}, api=api) }, api=api)
# TODO: Still single-threaded, could branch out to a few threads here to download in parallel. async def handle_texture_node(texture_node):
texture_nodes = await get_nodes(parent_node_uuid=parent_node_uuid)
for texture_node in texture_nodes:
# Skip non-texture nodes, as we can't thumbnail them anyway. # Skip non-texture nodes, as we can't thumbnail them anyway.
if texture_node['node_type'] != 'texture': if texture_node['node_type'] != 'texture':
continue return
# Find the File that belongs to this texture node # Find the File that belongs to this texture node
pic_uuid = texture_node['picture'] pic_uuid = texture_node['picture']
@ -201,6 +199,13 @@ async def fetch_texture_thumbs(parent_node_uuid: str, desired_size: str,
loop.call_soon_threadsafe(functools.partial(thumbnail_loaded, file_desc, thumb_path)) loop.call_soon_threadsafe(functools.partial(thumbnail_loaded, file_desc, thumb_path))
# Download all texture nodes in parallel.
texture_nodes = await get_nodes(parent_node_uuid=parent_node_uuid)
# raises any exception from failed handle_texture_node() calls.
await asyncio.gather(*(handle_texture_node(texture_node)
for texture_node in texture_nodes))
print('Done downloading texture thumbnails') print('Done downloading texture thumbnails')