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
kidlfy
V2EX  ›  Python

pymongo 熟悉 mongo 的大佬帮忙看一下错误

  •  
  •   kidlfy · 2019-01-28 15:41:17 +08:00 · 1959 次点击
    这是一个创建于 1906 天前的主题,其中的信息可能已经有所发展或是发生改变。
    '''
    with MongoHandler.create() as base:
    base['media']['media_data_{}'.format(kind)].insert(post)
    self.logger.info("inserted to Media_data_{}".format(kind))

    '''

    操作 mongo 库只有这种插入的操作,但时不时会报如下错误:
    '''
    Cannot run getMore on cursor 112765575416, which was created in session 809cd1d1-d8bd-4dba-a80d-14868b85446f - 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=, in session e9ffe991-5468-4a4a-a1b6-9ff5be9406c0 - 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=

    '''
    度娘没查到,跪求大佬指点
    18 条回复    2019-01-28 17:18:34 +08:00
    kidlfy
        1
    kidlfy  
    OP
       2019-01-28 15:41:54 +08:00
    MongoHandler.create() 相当于 mongo 的一个 client
    warcraft1236
        2
    warcraft1236  
       2019-01-28 15:49:33 +08:00
    pymongo 好像不这么用吧......
    ChristopherWu
        3
    ChristopherWu  
       2019-01-28 15:50:18 +08:00
    @kidlfy `Cannot run getMore on cursor `这个错误通常是 mongo 驱动( pymongo )报的,在 find 完后,mongod 会给一个 cursor 游标 给驱动,驱动再用 getMore 的方法去拿更多的数据。

    你看看你有啥操作涉及到这部分的?感觉你不是在做正常的 insert
    yongzhong
        4
    yongzhong  
       2019-01-28 15:50:34 +08:00
    操作 mongo 不需要每次都创建 client 吧,常用方式是
    client = MongoClient('HOST','PORT')

    db = client['dbname']

    db1 = db['aaa']
    db2 = db['bbb']

    然后其他地方直接调用 db1.insert_one()就好了,你的报错看上去是竞争问题
    zhouxuchen
        5
    zhouxuchen  
       2019-01-28 15:56:06 +08:00
    pymongo 真的是这么用的么……

    https://api.mongodb.com/python/current/tutorial.html
    kidlfy
        6
    kidlfy  
    OP
       2019-01-28 15:57:01 +08:00
    @ChristopherWu 我是有个 find 的操作,但只是判断了一下 find.count() 是否等于 0 除此之外就没有了
    kidlfy
        7
    kidlfy  
    OP
       2019-01-28 15:59:58 +08:00
    @zhouxuchen 没懂 我错在哪了 和文档一样的啊 我这个只是把 client 封装成了类的静态方法
    Vegetable
        8
    Vegetable  
       2019-01-28 16:01:24 +08:00
    从报错信息和你的代码来看你是创建了多个连接导致的问题,因为没这么用过所以也没见过这个问题

    我建议你在 Mongohandler.create()返回的 base 做成单例,使用一个 client 而不要用多个,应该能解决这个问题
    janxin
        9
    janxin  
       2019-01-28 16:17:41 +08:00
    看起来像是 cursor 复用了吧...
    kidlfy
        10
    kidlfy  
    OP
       2019-01-28 16:28:58 +08:00
    @Vegetable 刚排查出了错误,根本原因是有个返回值在 cursor 结束之后调用了,不是多个连接的原因,感谢
    kidlfy
        11
    kidlfy  
    OP
       2019-01-28 16:29:24 +08:00
    @ChristopherWu 感谢大佬 解决了谢谢
    ChristopherWu
        12
    ChristopherWu  
       2019-01-28 16:34:49 +08:00
    @kidlfy 怎么解决的呀?
    kidlfy
        13
    kidlfy  
    OP
       2019-01-28 16:37:48 +08:00
    @ChristopherWu 在 find 之后有个 return 操作 但 cursor 结束了 就改了下这部分
    aaa5838769
        14
    aaa5838769  
       2019-01-28 16:38:07 +08:00
    你代码写错了把- -
    ChristopherWu
        15
    ChristopherWu  
       2019-01-28 16:48:31 +08:00
    @kidlfy 能不能分享一下你在干啥(以及代码)? 我觉得其他人都很好奇你在干嘛,是不是用错了 mongo (我觉得不是的)
    kidlfy
        16
    kidlfy  
    OP
       2019-01-28 17:10:50 +08:00
    @ChristopherWu 我在做实时新闻的爬取,开了协程,mongo 只是用来去重存储,之所以用 with 并且每次创建一个 client 只是为了保证安全,就这样。。。。
    ChristopherWu
        17
    ChristopherWu  
       2019-01-28 17:17:09 +08:00
    @kidlfy 那为啥你要手动处理 cursor 啊?这个不是 pymongo 处理好的吗?
    kidlfy
        18
    kidlfy  
    OP
       2019-01-28 17:18:34 +08:00
    @ChristopherWu 我没手动处理啊。。。是我代码的问题。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5726 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 03:05 · PVG 11:05 · LAX 20:05 · JFK 23:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.