<s> '''<STR_LIT>''' <EOL> from subprocess import PIPE , Popen <EOL> from threading import Thread , Lock <EOL> from warnings import warn <EOL> import time <EOL> from collections import deque <EOL> class AsyncPopen ( Popen ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , args , bufsize = <NUM_LIT:0> , executable = None , <EOL> stdin = None , stdout = None , stderr = None , <EOL> preexec_fn = None , close_fds = False , shell = False , <EOL> cwd = None , env = None , universal_newlines = False , <EOL> startupinfo = None , creationflags = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> if stdin != None and stdin != PIPE : <EOL> warn ( "<STR_LIT>" ) <EOL> _stdin = None <EOL> else : <EOL> _stdin = stdin <EOL> if stdout != None and stdout != PIPE : <EOL> warn ( "<STR_LIT>" ) <EOL> _stdout = None <EOL> else : <EOL> _stdout = stdout <EOL> if stderr != None and stderr != PIPE : <EOL> warn ( "<STR_LIT>" ) <EOL> _stderr = None <EOL> else : <EOL> _stderr = stderr <EOL> super ( AsyncPopen , self ) . __init__ ( args , bufsize = bufsize , <EOL> executable = executable , stdin = _stdin , <EOL> stdout = _stdout , stderr = _stderr , <EOL> preexec_fn = preexec_fn , <EOL> close_fds = close_fds , <EOL> shell = shell , cwd = cwd , env = env , <EOL> universal_newlines = universal_newlines , <EOL> startupinfo = startupinfo , <EOL> creationflags = creationflags ) <EOL> self . use_stdout = False <EOL> '''<STR_LIT>''' <EOL> self . use_stderr = False <EOL> '''<STR_LIT>''' <EOL> self . use_stdin = False <EOL> '''<STR_LIT>''' <EOL> if _stdout == PIPE : <EOL> self . use_stdout = True <EOL> self . stdout_queue = deque ( ) <EOL> '''<STR_LIT>''' <EOL> self . stdout_lock = Lock ( ) <EOL> '''<STR_LIT>''' <EOL> self . stdout_thread = Thread ( target = self . _ThreadedOutputQueue , <EOL> args = ( self . stdout , self . stdout_queue , <EOL> self . stdout_lock ) ) <EOL> '''<STR_LIT>''' <EOL> self . stdout_thread . daemon = True <EOL> self . stdout_thread . start ( ) <EOL> if _stderr == PIPE : <EOL> self . use_stderr = True <EOL> self . stderr_queue = deque ( ) <EOL> '''<STR_LIT>''' <EOL> self . stderr_lock = Lock ( ) <EOL> '''<STR_LIT>''' <EOL> self . stderr_thread = Thread ( target = self . _ThreadedOutputQueue , <EOL> args = ( self . stderr , self . stderr_queue , <EOL> self . stderr_lock ) ) <EOL> '''<STR_LIT>''' <EOL> self . stderr_thread . daemon = True <EOL> self . stderr_thread . start ( ) <EOL> if _stdin == PIPE : <EOL> self . use_stdin = True <EOL> self . stdin_queue = deque ( ) <EOL> '''<STR_LIT>''' <EOL> self . stdin_lock = Lock ( ) <EOL> '''<STR_LIT>''' <EOL> self . stdin_thread = Thread ( target = self . _ThreadedInputQueue , <EOL> args = ( self . stdin , self . stdin_queue , <EOL> self . stdin_lock ) ) <EOL> '''<STR_LIT>''' <EOL> self . stdin_thread . daemon = True <EOL> self . stdin_thread . start ( ) <EOL> def _ThreadedOutputQueue ( self , pipe , queue , lock ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> while True : <EOL> chunk = pipe . readline ( ) <EOL> if not chunk : <EOL> break <EOL> lock . acquire ( ) <EOL> queue . append ( chunk ) <EOL> lock . release ( ) <EOL> time . sleep ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> finally : <EOL> pipe . close ( ) <EOL> def _ThreadedInputQueue ( self , pipe , queue , lock ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> while True : <EOL> lock . acquire ( ) <EOL> while len ( queue ) > <NUM_LIT:0> : <EOL> chunk = queue . popleft ( ) <EOL> pipe . write ( chunk ) <EOL> lock . release ( ) <EOL> pipe . flush ( ) <EOL> time . sleep ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> finally : <EOL> pipe . close ( ) <EOL> def communicate ( self , input = None ) : <EOL> '''<STR_LIT>''' <EOL> if self . use_stdin and input : <EOL> self . stdin_lock . acquire ( ) <EOL> self . stdin_queue . append ( input ) <EOL> self . stdin_lock . release ( ) <EOL> stdoutdata = None <EOL> stderrdata = None <EOL> if self . use_stdout : <EOL> data = b"<STR_LIT>" <EOL> self . stderr_lock . acquire ( ) <EOL> try : <EOL> while len ( self . stdout_queue ) > <NUM_LIT:0> : <EOL> data += self . stdout_queue . popleft ( ) <EOL> except : <EOL> self . stderr_lock . release ( ) <EOL> raise <EOL> self . stderr_lock . release ( ) <EOL> if data : stdoutdata = data <EOL> if self . use_stderr : <EOL> data = b"<STR_LIT>" <EOL> self . stderr_lock . acquire ( ) <EOL> try : <EOL> while len ( self . stderr_queue ) > <NUM_LIT:0> : <EOL> data += self . stderr_queue . popleft ( ) <EOL> except : <EOL> self . stderr_lock . release ( ) <EOL> raise <EOL> self . stderr_lock . release ( ) <EOL> if data : stderrdata = data <EOL> return ( stdoutdata , stderrdata ) </s>
