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

WIP: basic coroutine and subprocess support #393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bfredl
Copy link
Member

@bfredl bfredl commented May 5, 2019

Work towards #342

The model supported here is: plugin code is written as greenlets ( spawned as request handlers or nvim.async_call), but once in a while needs to invoke a coroutine defined by some library (such as asyncio itself). This model is enough is to use the io facilities of asyncio, which mostly uses raw callbacks.

nvim.run_coroutine(coro) will spawn an asyncio task and suspend the current greenlet until completion and return the result. If you don't care about the result you can still use the non-blocking asyncio.ensure_future(coro).

A simple example to suspend the current handler for some time (NB: will block nvim if the handler is sync):

c = asyncio.sleep(3)
nvim.run_coroutine(c)
nvim.command("echomsg 'done'")

As a convenience a wrapper for the subprocess protocol is also added, which wraps the data handler in a greenlet (so that it can use nvim requests):

def on_out(fd, data):
    nvim.out_write(data)

def on_exit():
    pass

transport = nvim.start_subprocess(['echo', 'test'], on_data=on_out, on_exit=on_exit)
# transport.write(b'bytes')

@Shougo
Copy link
Contributor

Shougo commented May 6, 2019

I have tested it and it works.

But

nvim.run_coroutine(c)

It must be nvim.run_coro(c) instead.

@bfredl
Copy link
Member Author

bfredl commented May 6, 2019

I forgot to push the last fix. I also fixed stdin capability: start_subprocess returns transport with .write() method.

@Shougo
Copy link
Contributor

Shougo commented May 6, 2019

Ah, OK. Thank you for the fix.

@Shougo
Copy link
Contributor

Shougo commented May 6, 2019

Hm....

I have tested it. The sleep works, but in subprocess cannot access neovim until RPC is finished.
It is intended behavior?

@Shougo
Copy link
Contributor

Shougo commented May 6, 2019

As a convenience a wrapper for the subprocess protocol is also added, which wraps the data handler in a greenlet (so that it can use nvim requests):

Hm. This is needed.

@bfredl
Copy link
Member Author

bfredl commented May 6, 2019

The sleep works, but in subprocess cannot access neovim until RPC is finished.

@Shougo What is the situation more specifically? If the parent python process is in a sync handler, the child cannot connect until the sync handler returns.

@Shougo
Copy link
Contributor

Shougo commented May 6, 2019

Shougo/denite.nvim#624

In the branch start_subprocess works?
The subprocess must use nvim object.

The sample seems use nvim in only on_out handler.

@bfredl
Copy link
Member Author

bfredl commented May 6, 2019

The subprocess can use nvim, but for now it has to connect on its own using $NVIM_LISTEN_ADDRESS. I don't think a socket handle can be duplicated in a portable way (I think libuv can transmit a handle over a socket, but it will basically be: on linux do X and on winNT do Y).

@Shougo
Copy link
Contributor

Shougo commented May 6, 2019

@Shougo What is the situation more specifically? If the parent python process is in a sync handler, the child cannot connect until the sync handler returns.

Yes. But in nvim.run_coroutine(c), cannot free the lock?

@bfredl
Copy link
Member Author

bfredl commented May 6, 2019

This is a bit tricky, parent and child python cannot collectively "own" the nvim focus, only one channel can be blocking at a time. For now it has to be async. We can of course add this ability, but it will need more code in nvim core.

@Shougo
Copy link
Contributor

Shougo commented May 6, 2019

For now it has to be async.

Yes, ui branch uses async RPC. But it is not perfect.

We can of course add this ability, but it will need more code in nvim core.

OK. So I wait it.

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

Successfully merging this pull request may close these issues.

2 participants