Skip to content

Instantly share code, notes, and snippets.

@rhee
Forked from rhee-elten/proc_stdout_gen.py
Created May 16, 2022 01:08
Show Gist options
  • Save rhee/18eedc6b35644e5c61af3244ad5739e4 to your computer and use it in GitHub Desktop.
Save rhee/18eedc6b35644e5c61af3244ad5739e4 to your computer and use it in GitHub Desktop.
## gen() function for proc.stdout ( run_commands, vdl_run ) ## async read from proc.stdout
## 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