Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for other async frameworks #77

Open
7 of 9 tasks
dhirschfeld opened this issue Jun 21, 2023 · 10 comments
Open
7 of 9 tasks

Add support for other async frameworks #77

dhirschfeld opened this issue Jun 21, 2023 · 10 comments
Labels
enhancement New feature or request

Comments

@dhirschfeld
Copy link

dhirschfeld commented Jun 21, 2023

Which project are you requesting an enhancement for?

kr8s

What do you need?

Rather than directly using asyncio primitives which preclude the use of other async framweorks it would be great if kr8s could utilise anyio instead so that users are free to use the async framework of their choice in their applications.

Tasks

  1. 1 of 4
    enhancement kr8s
  2. hygiene
@dhirschfeld dhirschfeld added the enhancement New feature or request label Jun 21, 2023
@dhirschfeld
Copy link
Author

Also, this looks super interesting! ❤️

I've felt the pain of trying to use the existing awkward, low-level apis so I'm very keen to give kr8s a go! 🚀

@jacobtomlinson
Copy link
Member

That sounds great, I think we should be able to swap anyio in.

@jacobtomlinson
Copy link
Member

Looking more into this it may be more complex than I first assumed, we currently depend on aiohttp, asyncio-atexit, aiofiles and async-timeout. It's not clear whether any of those would work with trio or another async framework, my initial searches make me think that none of them would work.

I think I would need to replace aiohttp with something like httpx and then find a replacement for the others or just vendor in the features we need.

@dhirschfeld
Copy link
Author

Hmm, right 🤔

  • aiohttp --> httpx would be the way to go there.
  • I think for file IO support you'd just use AnyIO's implementation but it looks like it might require some refactoring.

I'm not sure about the others, but will have a poke around to see what I can find out...

@dhirschfeld
Copy link
Author

I asked the experts on gitter.

It seems you might be able to get away with using AnyIO's native cancellation handling rather than async-timeout, though there's an argument there that anyio support should probably be pushed upstream.

Regarding asyncio-atexit there were questions about why it's even needed but Joshua Oreman came up with a recipe you could use:

def register_async_exit(callback):
    if sniffio.current_async_library() == "asyncio":
        import asyncio_atexit
        asyncio_atexit.register(callback)
    else:
        @trio.lowlevel.spawn_system_task
        async def cleanup_task():
            try:
                await trio.sleep_forever()
            finally:
                result = callback()
                if inspect.isawaitable(result):
                    with trio.move_on_after(5) as scope:
                        scope.shield = True
                        await result

change the timeout depending on how long the cleanup should be allowed to go for. asyncio-atexit doesn't use a timeout, I guess you could skip one here too (with trio.CancelScope(shield=True):), but that's usually a bad idea

@jacobtomlinson
Copy link
Member

jacobtomlinson commented Jun 27, 2023

Thanks @dhirschfeld this is really helpful.

I like the idea of using the timeouts from anyio and replacing aiofiles with the file handling in anyio because that would reduce our number of dependencies. Ideally, I'd like to keep the dependency tree here as light as possible so this aligns with that goal too.

We use asyncio-atexit to cleanly close the aiohttp session via a finalizer in case it hasn't been closed by the user. We may just be able to work around this altogether.

@jacobtomlinson
Copy link
Member

jacobtomlinson commented Jul 4, 2023

Quick update here. The core of kr8s now uses httpx and anyio so most operations should now work when run with trio. We have a few tests to verify this. The only exception is port forwards which still use aiohttp. They will be more gnarly to migrate to so I've left those for a separate PR (tracked by #104).

I also want to rename kr8s.asyncio to kr8s.async which is a large breaking change but I think renaming it will make things clearer that you can use it with trio. I intend to do the renaming and then cut a new release, then I'll handle the port forwards in a follow-up release.

@dhirschfeld
Copy link
Author

Sounds great! ❤️

I'm not sure if naming a module async might cause issues. If it does, aio might be an alternative, though I'm also fine with asyncio.

@jacobtomlinson
Copy link
Member

Ok fair enough, in that case I think I'll just leave it as kr8s.asyncio.

@jacobtomlinson
Copy link
Member

Ok 0.8.0 is out and is mostly trio compatible (apart from port forwards).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants