Discussion forum for David Beazley

Curio and blocking io operation


I started using curio and a lot of questions arised.
I have add method of PhotoManager class, which conceptually looks like this:

async def add(self, path):
    """ A lot of possible blocking operations are involved """
    # This is object with async initialization through internal _init()
    self.location = AsyncFactory.create(Location, path)
    full_path = self.location.path

    if full_path.is_dir():
        paths = full_path.glob("**")

        for item in paths:
            path = item.resolve()

            if path.is_file():
                    photo = Photo(path)

I realized that some functions I want to make as coroutines through async syntax use in body some mix of coroutines and blocking operations, like path.glob("**") and path.is_dir().

First I started to wrapping them into a wrapper function with decorator @async_thread but them I’ve decided just to change whole add as @async_thread

Is that possible to call coroutines inside @async_thread functions? Or how this should be handled?


You can call any coroutine inside an async thread using AWAIT(). For example:

async def coro(x, y):
      ... some coroutine ...

def func():
    result = AWAIT(coro(x,y))

This should work for any coroutine that you define as well as any feature of Curio that would normally require an await.