I'm trying to compare execution times for redis and aioredis in python using below 2 programs
## Program1.py - using python redis r.set('foo','bar') 10000 times
import datetime
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
begin_time = datetime.datetime.now()
for i in range(10000):
r.set('foo', 'bar')
print(datetime.datetime.now() - begin_time)
## Execution Time : 0:00:00.605273 seconds
## Program2.py - using python aioredis await self.redis.set('foo', 'bar') 10000 times
import asyncio
import aioredis
import time
import datetime
class Handler:
def __init__(self):
self.redis = None
self.loop = asyncio.get_event_loop()
self.loop.run_until_complete(self.__async__connect())
async def __async__connect(self):
self.redis = await aioredis.create_redis_pool('redis://localhost',db=0)
def command(self):
return self.loop.run_until_complete(self.__async__command())
async def __async__command(self):
await self.redis.set('foo', 'bar')
handler = Handler()
begin_time = datetime.datetime.now()
for i in range(10000):
done = handler.command()
print(datetime.datetime.now() - begin_time)
handler.redis.close()
## Execution Time : 0:00:01.738212 seconds
Question 1: Even after using aioredis, why does my Program2.py take more execution time than Program1.py.
Question 2: What can be done to effectively use asyncio/aioredis in Program2.py,so Program2.py runs faster then Program1.py