nvim-oxi
nvim-oxi copied to clipboard
Async support
Just to give a little bit of context, Rust's async functions return Future
s that need an executor to drive them to completion. The usual way to achieve this is to let the async runtime, e.g. tokio, control the "main" thread. This is not a viable option in our case since it would block the UI thread, i.e. the one Neovim runs in, defeating the whole point of concurrent code.
However it should be possible (emphasis on the should) to use libuv, which Neovim uses for the event loop, as the executor. We could expose something like a Task
object that wraps an async {}
block and drives it to completion by scheduling it via libuv.
can't you just run tokio on another thread ?
@momofor the lua state is thread local and gets initialized on the main thread when the plugin is require
d.
Looking through this it looks like there is an existing libuv wrapper for Rust called libuv-rs. If I am understanding this correctly, a Task
would be something that would accept an async function and we could schedule the function using something like a spawn to execute this in a new thread, is this what you have in mind?
we could schedule the function using something like a spawn to execute this in a new thread
Not in a new thread, in the main thread. That's what's tricky about it. If we could use other threads we could just use tokio, but those futures have to run on the same thread that Neovim runs in for the reason I described above.
Does this mean it is fine to run stuff asynchronously with Tokio as executor in another thread, as long as no interaction with Neovim is done in that thread? Or am I misunderstanding something?
@nik-x yes basically. Btw I'll probably close this issue as completed once I've polished abf70cbf2f5df2e4450f7578daf9008ec2548bd0 a bit. You can check out this example to see how it works.
Nice! Thank you
closed by 7d2e85a92a9b9fbe66dc72ac5af0e1bed0fcf28b