Skip to content

Commit a699fe7

Browse files
authored
Merge #545 refactor: asyncio eventloop
2 parents 5184647 + 17fbcbc commit a699fe7

File tree

11 files changed

+293
-263
lines changed

11 files changed

+293
-263
lines changed

pynvim/api/nvim.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Main Nvim interface."""
2+
3+
import asyncio
24
import os
35
import sys
46
import threading
@@ -140,7 +142,16 @@ def __init__(
140142
self._err_cb: Callable[[str], Any] = lambda _: None
141143
else:
142144
self._err_cb = err_cb
143-
self.loop = self._session.loop._loop
145+
146+
@property
147+
def loop(self) -> asyncio.AbstractEventLoop:
148+
"""Get the event loop (exposed to rplugins).""" # noqa
149+
150+
# see #294: for python 3.4+, the only available and guaranteed
151+
# implementation of msgpack_rpc BaseEventLoop is the AsyncioEventLoop.
152+
# The underlying asyncio event loop is exposed to rplugins.
153+
# pylint: disable=protected-access
154+
return self._session.loop._loop # type: ignore
144155

145156
def _from_nvim(self, obj: Any, decode: Optional[TDecodeMode] = None) -> Any:
146157
if decode is None:

pynvim/msgpack_rpc/async_session.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
"""Asynchronous msgpack-rpc handling in the event loop pipeline."""
22
import logging
33
from traceback import format_exc
4+
from typing import Any, AnyStr, Callable, Dict
45

6+
from pynvim.msgpack_rpc.msgpack_stream import MsgpackStream
57

68
logger = logging.getLogger(__name__)
79
debug, info, warn = (logger.debug, logger.info, logger.warning,)
810

911

10-
class AsyncSession(object):
12+
# response call back takes two arguments: (err, return_value)
13+
ResponseCallback = Callable[..., None]
14+
15+
16+
class AsyncSession:
1117

1218
"""Asynchronous msgpack-rpc layer that wraps a msgpack stream.
1319
@@ -16,11 +22,11 @@ class AsyncSession(object):
1622
requests and notifications.
1723
"""
1824

19-
def __init__(self, msgpack_stream):
25+
def __init__(self, msgpack_stream: MsgpackStream):
2026
"""Wrap `msgpack_stream` on a msgpack-rpc interface."""
2127
self._msgpack_stream = msgpack_stream
2228
self._next_request_id = 1
23-
self._pending_requests = {}
29+
self._pending_requests: Dict[int, ResponseCallback] = {}
2430
self._request_cb = self._notification_cb = None
2531
self._handlers = {
2632
0: self._on_request,
@@ -33,7 +39,8 @@ def threadsafe_call(self, fn):
3339
"""Wrapper around `MsgpackStream.threadsafe_call`."""
3440
self._msgpack_stream.threadsafe_call(fn)
3541

36-
def request(self, method, args, response_cb):
42+
def request(self, method: AnyStr, args: Any,
43+
response_cb: ResponseCallback) -> None:
3744
"""Send a msgpack-rpc request to Nvim.
3845
3946
A msgpack-rpc with method `method` and argument `args` is sent to
@@ -89,8 +96,9 @@ def _on_request(self, msg):
8996
# - msg[2]: method name
9097
# - msg[3]: arguments
9198
debug('received request: %s, %s', msg[2], msg[3])
92-
self._request_cb(msg[2], msg[3], Response(self._msgpack_stream,
93-
msg[1]))
99+
assert self._request_cb is not None
100+
self._request_cb(msg[2], msg[3],
101+
Response(self._msgpack_stream, msg[1]))
94102

95103
def _on_response(self, msg):
96104
# response to a previous request:
@@ -105,6 +113,7 @@ def _on_notification(self, msg):
105113
# - msg[1]: event name
106114
# - msg[2]: arguments
107115
debug('received notification: %s, %s', msg[1], msg[2])
116+
assert self._notification_cb is not None
108117
self._notification_cb(msg[1], msg[2])
109118

110119
def _on_invalid_message(self, msg):
@@ -113,15 +122,14 @@ def _on_invalid_message(self, msg):
113122
self._msgpack_stream.send([1, 0, error, None])
114123

115124

116-
class Response(object):
117-
125+
class Response:
118126
"""Response to a msgpack-rpc request that came from Nvim.
119127
120128
When Nvim sends a msgpack-rpc request, an instance of this class is
121129
created for remembering state required to send a response.
122130
"""
123131

124-
def __init__(self, msgpack_stream, request_id):
132+
def __init__(self, msgpack_stream: MsgpackStream, request_id: int):
125133
"""Initialize the Response instance."""
126134
self._msgpack_stream = msgpack_stream
127135
self._request_id = request_id

pynvim/msgpack_rpc/event_loop/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Event loop abstraction subpackage.
22
3-
Tries to use pyuv as a backend, falling back to the asyncio implementation.
3+
We use python's built-in asyncio as the backend.
44
"""
55

66
from pynvim.msgpack_rpc.event_loop.asyncio import AsyncioEventLoop as EventLoop

0 commit comments

Comments
 (0)