Creates a new instance that sends and receives messages over a socket.
(cls, sock, name=None)
| 87 | |
| 88 | @classmethod |
| 89 | def from_socket(cls, sock, name=None): |
| 90 | """Creates a new instance that sends and receives messages over a socket.""" |
| 91 | sock.settimeout(None) # make socket blocking |
| 92 | if name is None: |
| 93 | name = repr(sock) |
| 94 | |
| 95 | # TODO: investigate switching to buffered sockets; readline() on unbuffered |
| 96 | # sockets is very slow! Although the implementation of readline() itself is |
| 97 | # native code, it calls read(1) in a loop - and that then ultimately calls |
| 98 | # SocketIO.readinto(), which is implemented in Python. |
| 99 | socket_io = sock.makefile("rwb", 0) |
| 100 | |
| 101 | # SocketIO.close() doesn't close the underlying socket. |
| 102 | def cleanup(): |
| 103 | try: |
| 104 | sock.shutdown(socket.SHUT_RDWR) |
| 105 | except Exception: # pragma: no cover |
| 106 | pass |
| 107 | sock.close() |
| 108 | |
| 109 | return cls(socket_io, socket_io, name, cleanup) |
| 110 | |
| 111 | def __init__(self, reader, writer, name=None, cleanup=lambda: None): |
| 112 | """Creates a new JsonIOStream. |
no test coverage detected