Skip to content

Commit 60d334e

Browse files
Make kernel independent of zmq (#45)
1 parent 9d71b81 commit 60d334e

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

src/akernel/akernel.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from .connect import connect_channel
1010
from .kernel import Kernel
1111
from .kernelspec import write_kernelspec
12-
from .message import receive_message, send_message
1312

1413

1514
cli = typer.Typer()
@@ -106,34 +105,34 @@ async def start(self) -> None:
106105

107106
async def to_shell(self) -> None:
108107
while True:
109-
msg = await receive_message(self.shell_channel)
108+
msg = await self.shell_channel.arecv_multipart().wait()
110109
await self._to_shell_send_stream.send(msg)
111110

112111
async def from_shell(self) -> None:
113112
async for msg in self._from_shell_receive_stream:
114-
await send_message(msg, self.shell_channel)
113+
await self.shell_channel.asend_multipart(msg, copy=True).wait()
115114

116115
async def to_control(self) -> None:
117116
while True:
118-
msg = await receive_message(self.control_channel)
117+
msg = await self.control_channel.arecv_multipart().wait()
119118
await self._to_control_send_stream.send(msg)
120119

121120
async def from_control(self) -> None:
122121
async for msg in self._from_control_receive_stream:
123-
await send_message(msg, self.control_channel)
122+
await self.control_channel.asend_multipart(msg, copy=True).wait()
124123

125124
async def to_stdin(self) -> None:
126125
while True:
127-
msg = await receive_message(self.stdin_channel)
126+
msg = await self.stdin_channel.arecv_multipart().wait()
128127
await self._to_stdin_send_stream.send(msg)
129128

130129
async def from_stdin(self) -> None:
131130
async for msg in self._from_stdin_receive_stream:
132-
await send_message(msg, self.stdin_channel)
131+
await self.stdin_channel.asend_multipart(msg, copy=True).wait()
133132

134133
async def from_iopub(self) -> None:
135134
async for msg in self._from_iopub_receive_stream:
136-
await send_message(msg, self.iopub_channel)
135+
await self.iopub_channel.asend_multipart(msg, copy=True).wait()
137136

138137

139138
if __name__ == "__main__":

src/akernel/kernel.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from akernel.display import display
1515
import akernel.IPython
1616
from akernel.IPython import core
17-
from .connect import connect_channel
1817
from .message import create_message, feed_identities, deserialize, serialize
1918
from .execution import pre_execute, cache_execution
2019
from .traceback import get_traceback

src/akernel/message.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from __future__ import annotations
22

3+
import json
34
import uuid
45
import hmac
56
import hashlib
67
from datetime import datetime, timezone
78
from typing import Any, cast
89

9-
from zmq.utils import jsonapi
10-
from zmq_anyio import Socket
1110
from dateutil.parser import parse as dateutil_parse # type: ignore
1211

1312

@@ -97,24 +96,30 @@ def serialize(msg: dict[str, Any], key: str) -> list[bytes]:
9796
return to_send
9897

9998

100-
async def receive_message(sock: Socket) -> tuple[list[bytes], dict[str, Any]] | None:
101-
return await sock.arecv_multipart().wait()
102-
return None
99+
def dumps(o: Any, **kwargs) -> bytes:
100+
"""Serialize object to JSON bytes (utf-8).
103101
102+
Keyword arguments are passed along to :py:func:`json.dumps`.
103+
"""
104+
return json.dumps(o, **kwargs).encode("utf8")
104105

105-
async def send_message(
106-
msg: dict[str, Any],
107-
sock: Socket,
108-
) -> None:
109-
await sock.asend_multipart(msg, copy=True).wait()
106+
107+
def loads(s: bytes | str, **kwargs) -> dict | list | str | int | float:
108+
"""Load object from JSON bytes (utf-8).
109+
110+
Keyword arguments are passed along to :py:func:`json.loads`.
111+
"""
112+
if isinstance(s, bytes):
113+
s = s.decode("utf8")
114+
return json.loads(s, **kwargs)
110115

111116

112117
def pack(obj: dict[str, Any]) -> bytes:
113-
return jsonapi.dumps(obj)
118+
return dumps(obj)
114119

115120

116121
def unpack(s: bytes) -> dict[str, Any]:
117-
return cast(dict[str, Any], jsonapi.loads(s))
122+
return cast(dict[str, Any], loads(s))
118123

119124

120125
def sign(msg_list: list[bytes], key: str) -> bytes:

0 commit comments

Comments
 (0)