Skip to content

Synchronous calls from an asynchronous context #339

@numirias

Description

@numirias

I have a use case which I'd think is really common. From an asynchronous context I want to get the result of a synchronous call. E.g., in this asynchronous method I want to access the current buffer content:

class MyPlugin:

    @neovim.autocmd(..., sync=False)
    def my_async_cmd(self):
       # This obviously doesn't work because the function is async
       code = self._vim.current.buffer[:]

Of course, I could hack together my own synchronization construct using e.g. threading.Event:

import threading

class MyPlugin:
    ...
    def _wait_for(self, func):
        event = threading.Event()
        res = None
        def wrapper():
            nonlocal res
            res = func()
            event.set()
        self._vim.async_call(wrapper)
        event.wait()
        return res

With that I could now just use code = self._wait_for(lambda: self._vim.current.buffer[:]) to get the code, but this seems really sketchy and I feel there must be an elegant pattern (which would maybe be more obvious to me if I had a firmer grasp of greenlets).

Could you point me in the right direction here? (I've also read #229 but it doesn't seem to refer to the same problem.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions