Skip to content

Instantly share code, notes, and snippets.

@diyan
Created September 5, 2013 16:41
Show Gist options
  • Save diyan/6452766 to your computer and use it in GitHub Desktop.
Save diyan/6452766 to your computer and use it in GitHub Desktop.
Process runner which support stdout/stderr more than 65K
from __future__ import unicode_literals
import subprocess
from tempfile import TemporaryFile
import time
class Response(object):
"""Response from a remote command execution"""
def __init__(self, std_out, std_err, status_code):
self.std_out = std_out
self.std_err = std_err
self.status_code = status_code
def __repr__(self):
#TODO put tree dots at the end if out/err was truncated
return '<Response code {}, out "{}", err "{}">'.format(
self.status_code, self.std_out[:20], self.std_err[:20])
def run_cmd(args):
with TemporaryFile() as stdout_file, TemporaryFile() as stderr_file:
process = subprocess.Popen(args, stdout=stdout_file, stderr=stderr_file, stdin=subprocess.PIPE)
while process.poll() is None:
time.sleep(1)
stdout_file.seek(0)
stderr_file.seek(0)
stdout_str = stdout_file.read().rstrip()
stderr_str = stderr_file.read().rstrip()
return Response(stdout_str, stderr_str, process.returncode)
#NOTE that envoy supports following API.
#rs = run_cmd('ls -la')
rs = run_cmd(['ls', '-la'])
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment