Created
March 16, 2018 16:06
-
-
Save ecovictoriano/4faf0e2f27f41bb47d7f99b8a8c609aa to your computer and use it in GitHub Desktop.
This file contains 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
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