最近学习 aiohttp 。
想试试 python motor 单条插入的总体时间。没想到需要 160 多秒。 如果用 pymongo 单条插入,不用 aiohttp 和 motor,总体时间只需要 23 秒。如果用 insert_many,总体时间只需要 1.4 秒
请问各位大佬,是我 aiohttp 或者 motor 做错了什么?
import asyncio,hashlib,time
from aiohttp import web
import motor.motor_asyncio
motor_mongo_client = motor.motor_asyncio.AsyncIOMotorClient('127.0.0.1', 17177)
async def mongodb_test(request):
start_time = time.time()
for i in range(100000):
md5 = str(i) + u'a9cfb75778b38676'
md5 = md5.encode('utf-8')
md5 = hashlib.md5(md5).hexdigest()
items = {
'md5': md5,
'i': i
}
try:
await motor_mongo_client['mongodb_test']['insert_test'].insert_one(items)
except Exception as e:
print(e)
continue
end_time = time.time()
const_time = end_time - start_time
print(const_time)
return web.Response(text="done")
loop = asyncio.get_event_loop()
app = web.Application(loop=loop)
app.add_routes([
web.get('/index', index),
web.get('/mongodb_test',mongodb_test),
])
web.run_app(app, host='127.0.0.1', port=8080)