Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep, time
- import asyncio.subprocess
- import sys
- t0 = time()
- @asyncio.coroutine
- def start_process():
- script = """
- from time import sleep
- print('Lets count to 5')
- for i in range(1, 6):
- sleep(1)
- print(i)
- print('Good bye!')
- """
- # Create the subprocess, redirect the standard output into a pipe
- create = asyncio.create_subprocess_exec(sys.executable, '-c', script,
- stdout=asyncio.subprocess.PIPE,
- bufsize=0)
- proc = yield from create
- return proc
- @asyncio.coroutine
- def readline(proc):
- # Read one line of output
- data = yield from proc.stdout.readline()
- line = data.decode('ascii').rstrip()
- return line
- @asyncio.coroutine
- def follow_process():
- print('Starting Process')
- proc = yield from start_process()
- print('Following Process...')
- while proc.returncode is None:
- line = yield from readline(proc)
- sleep(0.01)
- if line:
- print(line)
- else:
- print('.', end='')
- print()
- print('Return Code', proc.returncode)
- if sys.platform == "win32":
- loop = asyncio.ProactorEventLoop()
- asyncio.set_event_loop(loop)
- else:
- loop = asyncio.get_event_loop()
- loop.run_until_complete(follow_process())
- print('done')
- loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement