From 3c484b22e35983f4108a5991f3d3c83401da65ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 20 Dec 2014 12:25:36 +0100 Subject: [PATCH] io handler task longer but more explicit: only one message is handled at a time --- rplugin/python/nvim_ipy.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/rplugin/python/nvim_ipy.py b/rplugin/python/nvim_ipy.py index 1a22349..89ebff1 100644 --- a/rplugin/python/nvim_ipy.py +++ b/rplugin/python/nvim_ipy.py @@ -75,6 +75,25 @@ def __init__(self, wraps): def __getattr__(self, name): return partial(self.wraps.vim.session.threadsafe_call, getattr(self.wraps, name)) +# FIXME: un-reinvent this wheel +class MsgQueue(object): + def __init__(self): + self.msgs = deque() + self.waiters = deque() + + def get(self): + while not self.msgs: + gr = greenlet.getcurrent() + self.waiters.append(gr) + gr.parent.switch() + return self.msgs.popleft() + + def put(self, msg): + self.msgs.append(msg) + if self.waiters: + handler = self.waiters.popleft() + handler.parent = greenlet.getcurrent() + handler.switch() @neovim.plugin class IPythonPlugin(object): @@ -82,10 +101,11 @@ def __init__(self, vim): self.vim = vim self.buf = None self.has_connection = False + self.pending_shell_msgs = {} + self.io_msgs = MsgQueue() + self.handle_io() - self.io_msgs = deque() - self.handling_io = False def configure(self): #FIXME: rethink the entire configuration interface thing @@ -270,15 +290,13 @@ def on_ipy_interrupt(self, args): def on_ipy_terminate(self, args): self.km.shutdown_kernel() - # this will be simpler when we switch to single-thread IO def on_iopub_msg(self, m): - self.io_msgs.append(m) - if self.handling_io: - return - self.handling_io = True - while self.io_msgs: - self._on_iopub_msg(self.io_msgs.popleft()) - self.handling_io = False + self.io_msgs.put(m) + + @ipy_async + def handle_io(self): + while True: + self._on_iopub_msg(self.io_msgs.get()) def _on_iopub_msg(self, m): #FIXME: figure out the smoothest way to to matchaddpos