简体   繁体   中英

Use asyncio with neovim remote plugins

I want to write a vim plugin that listens to Server-sent events . Since I am most fluent with python3 and use neovim, I figured it would be a good idea to use the neovim remote plugin API .

Obviously, listening for messages from the network must not be blocking, so asyncio must be involved somehow. But I was not able to figure out how to combine the two. Somewhere I would have to run an event loop. However, pynvim already runs its own event loop, so I probably should hook into that.

@pynvim.plugin
class MyPlugin:
    def __init__(self, nvim):
        self.nvim = nvim

    @pynvim.command('Connect', nargs='1')
    async def connect(self, args):
        url = base_url + args[0]
        async with sse_client.EventSource(url) as event_source:
            for raw in event_source:
                try:
                    msg = json.loads(raw)
                except json.JSONDecodeError:
                    continue
                do_something(msg)

This example doesn't work. The Connect command is not available in neovim (it was before I made it async).

Not sure if this is the best answer, but this is what I have found to work:

asyncio seems to keep a reference to the current loop, so asyncio.ensure_future() can be used to schedule async code. However, that async code will crash if it tries to access vim internals. In order to do that, you need to call yet another callback with nvim.async_call() .

@pynvim.plugin
class MyPlugin:
    def __init__(self, nvim):
        self.nvim = nvim

    async def _connect(self, url):
        async with sse_client.EventSource(url) as event_source:
            for raw in event_source:
                try:
                    msg = json.loads(raw)
                except json.JSONDecodeError:
                    continue
                self.nvim.async_call(do_something, msg)

    @pynvim.command('Connect', nargs='1')
    def connect(self, args):
        url = base_url + args[0]
        asyncio.ensure_future(self._connect(url))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM