# -*- coding: utf-8 -*- import asyncio @asyncio.coroutine def cmd_runner(cmd): # cmd = "ls" print(cmd) create = asyncio.create_subprocess_exec(cmd, shell=False, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE) proc = yield from create recv = yield from proc.stdout.readline() yield from proc.wait() yield from asyncio.sleep(2) print(recv) @asyncio.coroutine def consumer(loop, q): tasks = [] print("Consuming") while True: cmd = yield from q.get() if cmd == "q": break tasks.append(loop.create_task(cmd_runner(cmd))) yield from asyncio.wait(tasks) @asyncio.coroutine def producer(q): for x in range(10): print("Producing") yield from q.put("ls") yield from asyncio.sleep(0.5) yield from q.put("q") def main(): loop = asyncio.get_event_loop() q = asyncio.Queue(loop=loop) p = loop.create_task(producer(q)) c = loop.create_task(consumer(loop, q)) loop.run_until_complete(asyncio.wait([p, c])) loop.close() if __name__ == '__main__': main()