为什么用 Python motor 会慢这么多,我做错了什么吗? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
naldo0193
V2EX    Python

为什么用 Python motor 会慢这么多,我做错了什么吗?

  •  
  •   naldo0193 2020-09-01 11:08:00 +08:00 2806 次点击
    这是一个创建于 1868 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近学习 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) 
    5 条回复    2020-09-01 18:59:12 +08:00
    sivacohan
        1
    sivacohan  
    PRO
       2020-09-01 11:21:56 +08:00   1
    切换上下文是有成本的。
    await motor_mongo_client['mongodb_test']['insert_test'].insert_one(items)
    这一句会导致每一次执行到这,都会切换一下上下文。所以导致会慢。


    异步操作一般解决的是 IO 慢,导致整体并发不足的情况。在你的例子里,数据库 IO 并不是瓶颈,这个例子里,你应该使用多线程、或多进程。
    linw1995
        2
    linw1995  
       2020-09-01 13:23:28 +08:00 &nsp; 1
    motor 实际就是 pymongo + 多线程,每次操作都要另外个线程去做,完成再回调到主线程。而直接用 pymongo 就是单个线程在跑,少了上下文切换,当然快啦。并发高速度慢,并发低速度快,根据业务情况取舍吧
    bnm965321
        3
    bnm965321  
       2020-09-01 13:27:05 +08:00   1
    可以直接 ensure_future 还是什么函数,把协程插入到事件循环就行不要让下一个协程等待上一个执行完毕
    676529483
        4
    676529483  
       2020-09-01 15:58:18 +08:00   1
    你 for 循环里面用 await 就变成单线程了,试试 create_task
    Wincer
        5
    Wincer  
       2020-09-01 18:59:12 +08:00   1
    operatiOns= []
    for 循环内部 await 改成:
    operations.append(motor_mongo_client['mongodb_test']['insert_test'].insert_one(items))
    然后 asyncio.gather(*operations),这才是正确的使用方法
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5819 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 33ms UTC 01:35 PVG 09:35 LAX 18:35 JFK 21:35
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86