First approach to the python3.6 async programming, a simple consumer/producer (async programming with python 3.6 step 1)
As a first approach to the async programming I'm developing small scripts with python.
The main thing I need to learn is how to develop an asynchronous consumer/producer using an async Queue and python3.6.
First, a reproduction of the problem with synchronous programming, later the solution I found with async programming.
# Output
produce 0
produce 1
produce 2
... # imagin that
consume 7
consume 8
consume 9
2.005685806274414
It takes about 2 seconds
# Output
produce 0
produce 1
consume 0
produce 2
consume 1
produce 3
...
consume 7
produce 9
consume 8
consume 9
1.1065495014190674
It takes 1.10 seconds, pretty much better, no?
The main thing I need to learn is how to develop an asynchronous consumer/producer using an async Queue and python3.6.
First, a reproduction of the problem with synchronous programming, later the solution I found with async programming.
Synchronous
# First attempt, synchronous
from queue import Queue from time import sleep, perf_counter def produce(q: Queue, n: int): for i in range(n): print(f'produce {i}') q.put(i) sleep(0.1) def consume(q: Queue): for i in q.queue: print(f'consume {i}') sleep(0.1) start = perf_counter() q = Queue() produce(q, 10) consume(q) print(perf_counter() - start)
# Output
produce 0
produce 1
produce 2
... # imagin that
consume 7
consume 8
consume 9
2.005685806274414
It takes about 2 seconds
Asynchronous
# Second attempt, Asynchronous
import asyncio from time import perf_counter async def produce(queue: asyncio.Queue, n: int): for x in range(n): print(f'produce {x}') await asyncio.sleep(0.1) await queue.put(x) async def consume(queue: asyncio.Queue): while True: x = await queue.get() print(f'consume {x}') await asyncio.sleep(0.1) queue.task_done() async def run(n: int = 10): queue = asyncio.Queue() consumer = asyncio.ensure_future(consume(queue)) await produce(queue, n) await queue.join() consumer.cancel() start = perf_counter() loop = asyncio.get_event_loop() loop.run_until_complete(run()) loop.close() print(perf_counter() - start)
# Output
produce 0
produce 1
consume 0
produce 2
consume 1
produce 3
...
consume 7
produce 9
consume 8
consume 9
1.1065495014190674
It takes 1.10 seconds, pretty much better, no?
Comentarios
Publicar un comentario