Performance tune for pubsub socket

This commit is contained in:
Jake Howard 2018-12-08 13:07:26 +00:00
parent 2e354a3814
commit 29b2377a58
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -1,14 +1,17 @@
import os
import select import select
import socket import socket
import ujson import ujson
from ipc_unix.utils import DEFAULT_SOCKET_READ_TIMEOUT, read_payload, socket_has_data from ipc_unix.utils import read_payload, socket_has_data
class Subscriber: class Subscriber:
def __init__(self, socket_path): def __init__(self, socket_path):
self.socket_path = socket_path self.socket_path = socket_path
self.socket = socket.socket(socket.AF_UNIX, type=socket.SOCK_STREAM) self.socket = socket.socket(
socket.AF_UNIX, type=socket.SOCK_STREAM | socket.SOCK_NONBLOCK
)
self.socket.connect(self.socket_path) self.socket.connect(self.socket_path)
@property @property
@ -37,9 +40,11 @@ class Subscriber:
class Publisher: class Publisher:
def __init__(self, socket_path): def __init__(self, socket_path):
self.socket_path = socket_path self.socket_path = socket_path
self.master_socket = socket.socket(socket.AF_UNIX, type=socket.SOCK_STREAM) self.master_socket = socket.socket(
socket.AF_UNIX, type=socket.SOCK_STREAM | socket.SOCK_NONBLOCK
)
self.master_socket.bind(self.socket_path) self.master_socket.bind(self.socket_path)
self.master_socket.listen(1) self.master_socket.listen()
self.connections = [] self.connections = []
def close(self): def close(self):
@ -57,9 +62,7 @@ class Publisher:
def write(self, message: dict): def write(self, message: dict):
self.accept_new_connection() self.accept_new_connection()
_, writable, errorable = select.select( _, writable, errorable = select.select([], self.connections, [], 1)
[], self.connections, [], DEFAULT_SOCKET_READ_TIMEOUT
)
dead_sockets = [] dead_sockets = []