Improve tg frontend ipfs results gathering parallelism

pull/26/head
Guillermo Rodriguez 2023-10-08 10:02:15 -03:00
parent d749dc4f57
commit 16df97d731
No known key found for this signature in database
GPG Key ID: EC3AB66D5D83B392
3 changed files with 30 additions and 48 deletions

View File

@ -264,7 +264,6 @@ class SkynetGPUConnector:
ipfs_link_legacy = ipfs_link + '/image.png'
async with trio.open_nursery() as n:
async def get_and_set_results(link: str):
res = await get_ipfs_file(link, timeout=1)
logging.info(f'got response from {link}')

View File

@ -242,59 +242,43 @@ class SkynetTelegramFrontend:
ipfs_link_legacy = ipfs_link + '/image.png'
async def get_and_set_results(link: str):
results[link] = await get_ipfs_file(link)
res = await get_ipfs_file(link)
logging.info(f'got response from {link}')
if not res or res.status_code != 200:
logging.warning(f'couldn\'t get ipfs binary data at {link}!')
def get_image_from_resp(resp):
png_img = resp.raw
with Image.open(io.BytesIO(resp.raw)) as image:
else:
try:
with Image.open(io.BytesIO(res.raw)) as image:
w, h = image.size
if w > TG_MAX_WIDTH or h > TG_MAX_HEIGHT:
logging.warning(f'result is of size {image.size}')
image.thumbnail((TG_MAX_WIDTH, TG_MAX_HEIGHT))
tmp_buf = io.BytesIO()
image.save(tmp_buf, format='PNG')
png_img = tmp_buf.getvalue()
return png_img
results[link] = png_img
except UnidentifiedImageError:
logging.warning(f'couldn\'t get ipfs binary data at {link}!')
tasks = [
get_and_set_results(ipfs_link),
get_and_set_results(ipfs_link_legacy)
]
await asyncio.gather(*tasks)
png_img = None
if ipfs_link_legacy in results:
png_img = results[ipfs_link_legacy]
resp = results[ipfs_link_legacy]
if not resp or resp.status_code != 200:
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link_legacy}!')
if ipfs_link in results:
png_img = results[ipfs_link]
else:
try:
png_img = get_image_from_resp(resp)
except UnidentifiedImageError:
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link_legacy}!')
if not png_img:
resp = results[ipfs_link]
if not resp or resp.status_code != 200:
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link}!')
await self.update_status_message(
status_msg,
caption,
reply_markup=build_redo_menu(),
parse_mode='HTML'
)
return True
else:
try:
png_img = get_image_from_resp(resp)
except UnidentifiedImageError:
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link}!')
await self.update_status_message(
status_msg,
caption,

View File

@ -149,7 +149,6 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'):
user_config = {**user_row}
del user_config['id']
breakpoint()
if user_config['autoconf']:
user_config = perform_auto_conf(user_config)