loop = asyncio.get_event_loop()

tile = loop.run_until_complete(geoportail_wtms.download_ortho_photo(level, row, column))
# tile.to_pil_image().save(tile.filename(with_layer=True, with_level=True))

geoportail_map_provider = GeoPortailMapProvider(geoportail_wtms)
tasks = [asyncio.async(geoportail_map_provider.get_tile(level, row, column + i)) for i in range(3)]
loop.run_until_complete(asyncio.wait(tasks))

lru_cache = LruCache(constraint=1024**3)

def done_callback(future):
    print('done', future.result())

cached_pyramid = CachedPyramid(geoportail_map_provider, lru_cache)
tasks = [asyncio.async(cached_pyramid.acquire(level, row, column + i)) for i in (0, 1, 0)]
for task in tasks:
    task.add_done_callback(done_callback)
loop.run_until_complete(asyncio.wait(tasks))
cached_pyramid.release(level, row, column)
lru_cache.recycle()

# tiles = cached_pyramid.acquire_interval(level, mosaic_interval)

loop.close()

####################################################################################################
#
# End
#
geoportail_wtms = GeoPortailWTMS(user='******',
                                 password='******',
                                 api_key='qd58byg78dg3nloou4ksa0pz')

geoportail_pyramid = GeoPortailPyramid()

level = 16
longitude = GeoAngle(6, 7, 0)
latitude = GeoAngle(44, 41, 0)
location = GeoCoordinate(longitude, latitude)
row, column = geoportail_pyramid[level].coordinate_to_mosaic(location)
print(level, row, column)

geoportail_map_provider = GeoPortailMapProvider(geoportail_wtms)
lru_cache = LruCache(constraint=1024**3)
cached_pyramid = CachedPyramid(geoportail_map_provider, lru_cache)

loop = asyncio.get_event_loop()

def done_callback(future):
    # logger.info
    print('done ' + str(future.result()))

tasks = [asyncio.async(cached_pyramid.acquire(level, row, column + i)) for i in range(50)]
for task in tasks:
    task.add_done_callback(done_callback)
loop.run_until_complete(asyncio.wait(tasks))

loop.close()

####################################################################################################