Forum Archive

What is the proper way to make async http requests in Pythonista, without blocking UI and without ui.in_background (asyncio doesn’t allow it)?

idchlife

I’m making an app that will be used on 3 of my iPads via working copy and Pythonista. It will upload photos made in iPad to my server. Unfortunately, as I understand, requests is sync library, so ui thread will be locked.
I though about aiohttp, but as I can see there is no clear way how to install it into pythonista app repo and then install it on the other devices (like I would with pipenv on my server)

I wondered about ui.in_background, but my app already uses asyncio to the fullest (many ui updates, statuses via simple http requests to my server), so ui.in_background simply does not execute because asyncio.loop.run_until_completes does not give it opportunity to even start executing

Thanks!

JonB

aiohttp can easily be installed using stash + pip.

there is hardly ever any reason to use in_background, since those get queued up on the interpreter thread. you can define your own threading based decorator such as

def run_async(func):
   from threading import Thread
   from functools import wraps

   @wraps(func)
   def async_func(*args, **kwargs):
      func_hl = Thread(target = func, args = args, kwargs = kwargs)
      func_hl.start()
      return func_hl

   return async_func