Skip to content

Instantly share code, notes, and snippets.

@ecovictoriano
Created March 16, 2018 16:06
Show Gist options
  • Save ecovictoriano/4faf0e2f27f41bb47d7f99b8a8c609aa to your computer and use it in GitHub Desktop.
Save ecovictoriano/4faf0e2f27f41bb47d7f99b8a8c609aa to your computer and use it in GitHub Desktop.
def shell_exec(cmd, cb=None, poll=True):
"""execute a shell command
cmd=str command to execute
cb=<callable> callable function
poll=True whether to poll for updates"""
with Popen(cmd, stdout=PIPE, bufsize=1) as p:
if poll:
while p.poll() is None:
line = p.stdout.readline().decode('utf-8').strip()
if callable(cb): cb(line)
time.sleep(0.5)
for line in p.stdout.readlines():
line = line.decode('utf-8').rstrip()
if callable(cb): cb(line)
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment