Created
June 21, 2016 14:05
-
-
Save cr0hn/b8fe71ed6c9ad20bbef4574fd48ae06d to your computer and use it in GitHub Desktop.
Simple producer / consumer using native asyncio Python >= 3.3 module, using non-blocking Queues and non-blocking system commands calls
This file contains hidden or 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
# -*- 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment