Guest User

Untitled

a guest
Feb 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import sys
  2. import logging
  3. import shlex
  4. import asyncio
  5.  
  6.  
  7. async def _read_stream(stream, cb):
  8. while True:
  9. line = await stream.readline()
  10. if line:
  11. cb(line.decode())
  12. else:
  13. break
  14.  
  15.  
  16. async def _stream_subprocess(cmd, stdout_cb, stderr_cb):
  17. process = await asyncio.create_subprocess_exec(*cmd,
  18. stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
  19.  
  20. await asyncio.wait([
  21. _read_stream(process.stdout, stdout_cb),
  22. _read_stream(process.stderr, stderr_cb)
  23. ])
  24. return await process.wait()
  25.  
  26.  
  27. def execute(cmd, stdout_cb, stderr_cb):
  28. loop = asyncio.get_event_loop()
  29. rc = loop.run_until_complete(
  30. _stream_subprocess(
  31. cmd,
  32. stdout_cb,
  33. stderr_cb,
  34. ))
  35. loop.close()
  36. return rc
  37.  
  38.  
  39. def run_command(command,
  40. output_function=lambda x: print("STDOUT: %s" % x),
  41. error_handling=lambda x: print("STDERR: %s" % x),
  42. cwd=None):
  43. execute(shlex.split(command), output_function, error_handling, )
  44.  
  45.  
  46. if __name__ == "__main__":
  47. "Get the command on 1st position and run it"
  48. command_str = sys.argv[1]
  49. run_command(command_str)
  50.  
  51. #!/bin/bash
  52. for i in {1..5}
  53. do
  54. echo "$i"
  55. sleep 0.5
  56. done
  57.  
  58. import time
  59.  
  60. for i in range(1, 6):
  61. print(i)
  62. time.sleep(0.5)
Add Comment
Please, Sign In to add comment