From e48d7aa88ceafdf19524b9a4f931300f7f7b8233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Thu, 6 Dec 2012 22:58:57 +0100 Subject: [PATCH] Make sure stdin and stdout are non-blocking to avoid dead locks --- darc/remote.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/darc/remote.py b/darc/remote.py index 079be0e92..55597a9be 100644 --- a/darc/remote.py +++ b/darc/remote.py @@ -73,7 +73,7 @@ def __init__(self, name): self.name = name def __init__(self, location, create=False): - self.cache = LRUCache(200) + self.cache = LRUCache(256) self.to_send = '' self.extra = {} self.pending = {} @@ -84,6 +84,8 @@ def __init__(self, location, create=False): self.p = Popen(args, bufsize=0, stdin=PIPE, stdout=PIPE) self.stdin_fd = self.p.stdin.fileno() self.stdout_fd = self.p.stdout.fileno() + fcntl.fcntl(self.stdin_fd, fcntl.F_SETFL, fcntl.fcntl(self.stdin_fd, fcntl.F_GETFL) | os.O_NONBLOCK) + fcntl.fcntl(self.stdout_fd, fcntl.F_SETFL, fcntl.fcntl(self.stdout_fd, fcntl.F_GETFL) | os.O_NONBLOCK) self.r_fds = [self.stdout_fd] self.x_fds = [self.stdin_fd, self.stdout_fd] @@ -97,9 +99,10 @@ def __del__(self): def call(self, cmd, args, wait=True): self.msgid += 1 - self.p.stdin.write(msgpack.packb((1, self.msgid, cmd, args))) - while wait: - r, w, x = select.select(self.r_fds, [], self.x_fds, 1) + to_send = msgpack.packb((1, self.msgid, cmd, args)) + w_fds = [self.stdin_fd] + while wait or to_send: + r, w, x = select.select(self.r_fds, w_fds, self.x_fds, 1) if x: raise Exception('FD exception occured') if r: @@ -116,6 +119,13 @@ def call(self, cmd, args, wait=True): args = self.pending.pop(msgid, None) if args is not None: self.cache[args] = msgid, res, error + if w: + if to_send: + n = os.write(self.stdin_fd, to_send) + assert n > 0 + to_send = to_send[n:] + else: + w_fds = [] def _read(self): data = os.read(self.stdout_fd, BUFSIZE)