Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import logging
- import shlex
- import asyncio
- async def _read_stream(stream, cb):
- while True:
- line = await stream.readline()
- if line:
- cb(line.decode())
- else:
- break
- async def _stream_subprocess(cmd, stdout_cb, stderr_cb):
- process = await asyncio.create_subprocess_exec(*cmd,
- stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
- await asyncio.wait([
- _read_stream(process.stdout, stdout_cb),
- _read_stream(process.stderr, stderr_cb)
- ])
- return await process.wait()
- def execute(cmd, stdout_cb, stderr_cb):
- loop = asyncio.get_event_loop()
- rc = loop.run_until_complete(
- _stream_subprocess(
- cmd,
- stdout_cb,
- stderr_cb,
- ))
- loop.close()
- return rc
- def run_command(command,
- output_function=lambda x: print("STDOUT: %s" % x),
- error_handling=lambda x: print("STDERR: %s" % x),
- cwd=None):
- execute(shlex.split(command), output_function, error_handling, )
- if __name__ == "__main__":
- "Get the command on 1st position and run it"
- command_str = sys.argv[1]
- run_command(command_str)
- #!/bin/bash
- for i in {1..5}
- do
- echo "$i"
- sleep 0.5
- done
- import time
- for i in range(1, 6):
- print(i)
- time.sleep(0.5)
Add Comment
Please, Sign In to add comment