class SocketConnection(object): def __init__(self, *args, **kw): try: # py3k compat. from _multiprocessing import Connection except ImportError: from multiprocessing.connection import Connection self.conn = Connection(*args, **kw) def send_bytes(self, *args, **kw): return self.conn.send_bytes(*args, **kw) def recv_bytes(self, *args, **kw): return self.conn.recv_bytes(*args, **kw) def send(self, *args, **kw): return self.conn.send(*args, **kw) def recv(self, *args, **kw): return self.conn.recv(*args, **kw) def close(self, *args, **kw): return self.conn.close(*args, **kw) def sendarr(self, arr): self.send(arr) def recvarr(self, arr): arr[:] = self.recv()[:]
class ExecingProcess_ConnectionWrapper(object): """ Wrapper around _multiprocessing.Connection. This is needed to use our own Pickler. """ def __init__(self, fd=None): self.fd = fd if self.fd: from _multiprocessing import Connection self.conn = Connection(fd) def __getstate__(self): return self.fd def __setstate__(self, state): self.__init__(state) def __getattr__(self, attr): return getattr(self.conn, attr) def _check_closed(self): assert not self.conn.closed def _check_writable(self): assert self.conn.writable def _check_readable(self): assert self.conn.readable def send(self, value): self._check_closed() self._check_writable() buf = StringIO() Pickler(buf).dump(value) self.conn.send_bytes(buf.getvalue()) def recv(self): self._check_closed() self._check_readable() buf = self.conn.recv_bytes() f = StringIO(buf) return Unpickler(f).load()
def main(): import sys sessionDir = sys.argv[1] sessionName = sys.argv[2] from test_inline import Server from _multiprocessing import Connection Server(sessionDir, sessionName, Connection(0), Connection(1)).run() f.close()
def __init__(self, fd=None, conn=None): self.fd = fd if self.fd: self.conn = Connection(fd) elif conn: self.conn = conn else: self.conn = None
def __init__(self, fd=None, conn=None): self.fd = fd if self.fd: from _multiprocessing import Connection self.conn = Connection(fd) elif conn: self.conn = conn else: self.conn = None
def other_process_run(): sock_other = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock_other.bind(server_address) sock_other.listen(1) c, address = sock_other.accept() print "address=" + address fd = duplicate(c.fileno()) c.close() conn = Connection(fd) try: print conn.recv_bytes(10) finally: conn.close() print "other process exit"
class ExecingProcess_ConnectionWrapper(object): """ Wrapper around multiprocessing.connection.Connection. This is needed to use our own Pickler. """ def __init__(self, fd=None, conn=None): self.fd = fd if self.fd: self.conn = Connection(fd) elif conn: self.conn = conn else: self.conn = None def __repr__(self): if self.conn is not None: return "<ExecingProcess_ConnectionWrapper fileno=%r>" % self.conn.fileno() else: return "<ExecingProcess_ConnectionWrapper None>" def __getstate__(self): if self.fd is not None: return {"fd": self.fd} elif self.conn is not None: return {"conn": self.conn} # Try to pickle the connection. else: return {} def __setstate__(self, state): self.__init__(**state) def __getattr__(self, attr): return getattr(self.conn, attr) def _check_closed(self): if self.conn.closed: raise ProcConnectionDied("connection closed") def _check_writable(self): if not self.conn.writable: raise ProcConnectionDied("connection not writeable") def _check_readable(self): if not self.conn.readable: raise ProcConnectionDied("connection not readable") def poll(self, *args, **kwargs): while True: try: return self.conn.poll(*args, **kwargs) except IOError as e: if e.errno == errno.EINTR: # https://stackoverflow.com/questions/14136195 # We can just keep trying. continue raise ProcConnectionDied("poll IOError: %s" % e) except EOFError as e: raise ProcConnectionDied("poll EOFError: %s" % e) def send_bytes(self, value): try: self.conn.send_bytes(value) except (EOFError, IOError) as e: raise ProcConnectionDied("send_bytes EOFError/IOError: %s" % e) def send(self, value): self._check_closed() self._check_writable() buf = BytesIO() Pickler(buf).dump(value) self.send_bytes(buf.getvalue()) def recv_bytes(self): while True: try: return self.conn.recv_bytes() except IOError as e: if e.errno == errno.EINTR: # https://stackoverflow.com/questions/14136195 # We can just keep trying. continue raise ProcConnectionDied("recv_bytes IOError: %s" % e) except EOFError as e: raise ProcConnectionDied("recv_bytes EOFError: %s" % e) def recv(self): self._check_closed() self._check_readable() buf = self.recv_bytes() f = BytesIO(buf) res = Unpickler(f).load() return res
class ExecingProcess_ConnectionWrapper(object): """ Wrapper around _multiprocessing.Connection. This is needed to use our own Pickler. """ def __init__(self, fd=None, conn=None): self.fd = fd if self.fd: from _multiprocessing import Connection self.conn = Connection(fd) elif conn: self.conn = conn else: self.conn = None def __getstate__(self): if self.fd is not None: return {"fd": self.fd} elif self.conn is not None: return {"conn": self.conn} # Try to pickle the connection. else: return {} def __setstate__(self, state): self.__init__(**state) def __getattr__(self, attr): return getattr(self.conn, attr) def _check_closed(self): if self.conn.closed: raise ProcConnectionDied("connection closed") def _check_writable(self): if not self.conn.writable: raise ProcConnectionDied("connection not writeable") def _check_readable(self): if not self.conn.readable: raise ProcConnectionDied("connection not readable") def poll(self, *args, **kwargs): while True: try: return self.conn.poll(*args, **kwargs) except IOError as e: if e.errno == errno.EINTR: # http://stackoverflow.com/questions/14136195 # We can just keep trying. continue raise ProcConnectionDied("poll IOError: %s" % e) except EOFError as e: raise ProcConnectionDied("poll EOFError: %s" % e) def send_bytes(self, value): try: self.conn.send_bytes(value) except (EOFError, IOError) as e: raise ProcConnectionDied("send_bytes EOFError/IOError: %s" % e) def send(self, value): self._check_closed() self._check_writable() buf = BytesIO() Pickler(buf).dump(value) self.send_bytes(buf.getvalue()) def recv_bytes(self): while True: try: return self.conn.recv_bytes() except IOError as e: if e.errno == errno.EINTR: # http://stackoverflow.com/questions/14136195 # We can just keep trying. continue raise ProcConnectionDied("recv_bytes IOError: %s" % e) except EOFError as e: raise ProcConnectionDied("recv_bytes EOFError: %s" % e) def recv(self): self._check_closed() self._check_readable() buf = self.recv_bytes() f = BytesIO(buf) res = Unpickler(f).load() return res
def __init__(self, *args, **kw): try: # py3k compat. from _multiprocessing import Connection except ImportError: from multiprocessing.connection import Connection self.conn = Connection(*args, **kw)
def other_process_run(): sock_other = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock_other.bind(server_address) sock_other.listen(1) c, address = sock_other.accept() print "address=" + address fd = duplicate(c.fileno()) c.close() conn = Connection(fd) try: print conn.recv_bytes(10) finally: conn.close() print "other process exit" p = Process(target=other_process_run) p.start() sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sleep(1) sock.connect(server_address) fd = duplicate(sock.fileno()) conn = Connection(fd) sock.close() conn.send_bytes("hello") conn.close() p.join() print "main process exit"