This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
ipc-unix/ipc_unix/utils.py

25 lines
579 B
Python
Raw Normal View History

import select
2018-12-07 20:35:34 +00:00
import ujson
BUFFER_SIZE = 4096
DEFAULT_SOCKET_READ_TIMEOUT = 0.01
2018-12-08 14:36:37 +00:00
NEW_LINE = b"\n"
2018-12-08 01:15:07 +00:00
def socket_has_data(sock, timeout=DEFAULT_SOCKET_READ_TIMEOUT) -> bool:
readable, _, _ = select.select([sock], [], [], timeout)
return sock in readable
2018-12-07 20:35:34 +00:00
def read_payload(payload):
data = b""
2018-12-08 14:36:37 +00:00
while NEW_LINE not in data:
if not socket_has_data(payload):
break
message = payload.recv(BUFFER_SIZE)
2018-12-07 20:35:34 +00:00
if message == b"":
break
data += message
2018-12-08 14:36:37 +00:00
return [ujson.loads(row) for row in data.split(NEW_LINE) if row]