nvim-oxi icon indicating copy to clipboard operation
nvim-oxi copied to clipboard

Async support

Open noib3 opened this issue 1 year ago • 4 comments

Just to give a little bit of context, Rust's async functions return Futures 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.

noib3 avatar Jul 18 '22 22:07 noib3

can't you just run tokio on another thread ?

momofor avatar Jul 24 '22 06:07 momofor

@momofor the lua state is thread local and gets initialized on the main thread when the plugin is required.

noib3 avatar Jul 25 '22 08:07 noib3

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?

ethanholz avatar Jul 31 '22 03:07 ethanholz

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.

noib3 avatar Jul 31 '22 09:07 noib3

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?

niklaswimmer avatar Aug 17 '22 08:08 niklaswimmer

@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.

noib3 avatar Aug 17 '22 08:08 noib3

Nice! Thank you

niklaswimmer avatar Aug 17 '22 08:08 niklaswimmer

closed by 7d2e85a92a9b9fbe66dc72ac5af0e1bed0fcf28b

noib3 avatar Sep 03 '22 14:09 noib3