Use msgpack for quote-packet serialization

kivy_mainline_and_py3.8
Tyler Goodlet 2018-04-18 13:56:56 -04:00
parent dd5e1e7ea7
commit 51b44cf236
2 changed files with 8 additions and 17 deletions

View File

@ -3,12 +3,12 @@ Core broker-daemon tasks and API.
""" """
import time import time
import inspect import inspect
import json
from functools import partial from functools import partial
import socket import socket
from types import ModuleType from types import ModuleType
from typing import Coroutine from typing import Coroutine
import msgpack
import trio import trio
from ..log import get_logger from ..log import get_logger
@ -88,6 +88,7 @@ class StreamQueue:
""" """
delim = self._delim delim = self._delim
buff = b'' buff = b''
unpacker = msgpack.Unpacker(raw=False)
while True: while True:
packets = [] packets = []
try: try:
@ -101,23 +102,13 @@ class StreamQueue:
log.debug("Stream connection was closed") log.debug("Stream connection was closed")
return return
if buff: # last received packet was segmented unpacker.feed(data)
data = buff + data for packet in unpacker:
yield packet
# if last packet has not fully arrived it will
# be a truncated byte-stream
packets = data.split(delim)
buff = packets.pop()
for packet in packets:
try:
yield json.loads(packet)
except json.decoder.JSONDecodeError:
log.exception(f"Failed to process JSON packet: {buff}")
continue
async def put(self, data): async def put(self, data):
return await self.stream.send_all(json.dumps(data).encode() + b'\n') return await self.stream.send_all(
msgpack.dumps(data, use_bin_type=True))
async def get(self): async def get(self):
return await self._agen.asend(None) return await self._agen.asend(None)

View File

@ -36,7 +36,7 @@ setup(
}, },
install_requires=[ install_requires=[
'click', 'colorlog', 'trio', 'attrs', 'async_generator', 'click', 'colorlog', 'trio', 'attrs', 'async_generator',
'pygments', 'cython', 'asks', 'pandas', 'pygments', 'cython', 'asks', 'pandas', 'msgpack',
#'kivy', see requirement.txt; using a custom branch atm #'kivy', see requirement.txt; using a custom branch atm
], ],
extras_require={ extras_require={