From 7a2b9983f18089873a612d2b99e7c85231af19bd Mon Sep 17 00:00:00 2001 From: Steven Robertson Date: Mon, 23 Jul 2012 00:05:26 -0700 Subject: [PATCH] Non-blocking poll for encoder subprocess --- cuburn/output.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cuburn/output.py b/cuburn/output.py index 5f8f39d..28a5af5 100644 --- a/cuburn/output.py +++ b/cuburn/output.py @@ -16,6 +16,11 @@ if not hasattr(scipy.misc, 'toimage'): raise ImportError("Could not find scipy.misc.toimage. " "Are scipy and PIL installed?") +try: + import gevent +except ImportError: + gevent = None + def launchC(name, mod, stream, dim, fb, *args): launch(name, mod, stream, (32, 8, 1), (int(np.ceil(dim.w/32.)), int(np.ceil(dim.h/8.))), @@ -168,10 +173,20 @@ class X264Output(Output, ClsMod): self.zeros.fill(32767) def _flush_sub(self, subp): - (stdout, stderr) = subp.communicate() + if gevent is not None: + # Use non-blocking poll to allow applications to continue + # rendering in other coros + subp.stdin.close() + log = '' + while subp.poll() is None: + log += subp.stderr.read() + gevent.sleep(0.1) + log += subp.stderr.read() + else: + (stdout, log) = subp.communicate() if subp.returncode: raise IOError("x264 exited with an error") - return stderr + return log def _flush(self): if self.subp is None: @@ -185,7 +200,7 @@ class X264Output(Output, ClsMod): self.asubp = None return ({'_color.h264': self.outf, '_alpha.h264': self.aoutf}, [('x264_color', log), ('x264_alpha', alog)]) - return {'.h264': self.outf}, [('x264_color', stderr)] + return {'.h264': self.outf}, [('x264_color', log)] def _write(self, buf, subp): try: