From 9963224e58b8ed3e336d95699937943a300251cc Mon Sep 17 00:00:00 2001 From: Jeff Albrecht Date: Sat, 13 Jun 2020 19:34:53 -0500 Subject: [PATCH] remove asyncio callback (#52) --- aiocogeo/partial_reads.py | 49 +++++++++++++-------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/aiocogeo/partial_reads.py b/aiocogeo/partial_reads.py index 7bd36d7..044837e 100644 --- a/aiocogeo/partial_reads.py +++ b/aiocogeo/partial_reads.py @@ -208,28 +208,6 @@ def _init_array(self, img_tiles: TileMetadata) -> NpArrayType: fused = np.ma.masked_array(fused) return fused - @staticmethod - def _stitch_image_tile_callback( - fut: asyncio.Future, - fused_arr: NpArrayType, - idx: int, - idy: int, - tile_width: int, - tile_height: int, - ) -> None: - """Internal asyncio callback used to mosaic each image tile into a larger array (see ``_init_array``)""" - img_arr = fut.result() - fused_arr[ - :, - idy * tile_height : (idy + 1) * tile_height, - idx * tile_width : (idx + 1) * tile_width, - ] = img_arr - if np.ma.is_masked(img_arr): - fused_arr.mask[ - :, - idy * tile_height : (idy + 1) * tile_height, - idx * tile_width : (idx + 1) * tile_width, - ] = img_arr.mask @staticmethod def _stitch_image_tile( @@ -253,6 +231,21 @@ def _stitch_image_tile( idx * tile_width : (idx + 1) * tile_width, ] = arr.mask + + async def _get_and_stitch_tile( + self, + xtile: int, + ytile: int, + idx: int, + idy: int, + img_tiles: TileMetadata, + fused_arr: NpArrayType + ) -> None: + """Asynchronously request an internal tile and stitch into an array""" + tile = await self.get_tile(xtile, ytile, img_tiles.ovr_level) + self._stitch_image_tile(tile, fused_arr, idx, idy, img_tiles.tile_width, img_tiles.tile_height) + + async def _request_tiles(self, img_tiles: TileMetadata) -> NpArrayType: """Concurrently request the image tiles and mosaic into a larger array""" img_arr = self._init_array(img_tiles) @@ -260,17 +253,7 @@ async def _request_tiles(self, img_tiles: TileMetadata) -> NpArrayType: for idx, xtile in enumerate(range(img_tiles.xmin, img_tiles.xmax + 1)): for idy, ytile in enumerate(range(img_tiles.ymin, img_tiles.ymax + 1)): get_tile_task = asyncio.create_task( - self.get_tile(xtile, ytile, img_tiles.ovr_level) - ) - get_tile_task.add_done_callback( - partial( - self._stitch_image_tile_callback, - fused_arr=img_arr, - idx=idx, - idy=idy, - tile_width=img_tiles.tile_width, - tile_height=img_tiles.tile_height, - ) + self._get_and_stitch_tile(xtile, ytile, idx, idy, img_tiles, img_arr) ) tile_tasks.append(get_tile_task) await asyncio.gather(*tile_tasks)