Created
May 16, 2022 00:59
-
-
Save rhee-elten/6f78d1a432bdfff3b6950d00a090aeea to your computer and use it in GitHub Desktop.
## gen() function for proc.stdout ( run_commands, vdl_run ) ## async read from proc.stdout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## gen() function for proc.stdout ( run_commands, vdl_run ) | |
## async read from proc.stdout | |
if True: | |
def gen(proc): | |
remains = b'' # remaining from prev read | |
while proc.poll() is None: | |
rlist, _, _ = select([proc.stdout],[],[],500) | |
if proc.stdout not in rlist: | |
continue | |
data = os.read(proc.stdout.fileno(), 16384) | |
if not data: ## EOF (empty bytes) | |
if remains: # flush remains, if exists | |
yield _decode(remains) | |
if verbose: | |
print('\n\ngen([%d]): EOF'.format(proc.pid)) | |
break # break loop after flush | |
data = remains + data | |
remains = b'' | |
for ln in data.splitlines(keepends=True): | |
if ln.endswith(b'\n'): | |
yield _decode(ln) | |
else: | |
remains = ln | |
try: | |
outs, errs = proc.communicate() | |
yield _decode(outs) | |
if errs: | |
yield _decode(errs) | |
except ValueError: # I/O operation on closed file // PyMemoryView_FromBuffer(): info->buf must not be NULL | |
pass | |
assert proc.returncode == 0, ('subprocess exit with:', proc.returncode) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment